當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
JavaScript MVC之Jamal
生活随笔
收集整理的這篇文章主要介紹了
JavaScript MVC之Jamal
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
.為什么要搞JavaScript MVC了 ? 有些人可能不理解, 我剛開始也不理解,但是有一些現實里的需求,讓我主動去尋找一個這樣的MVC框架, 我的需求很簡單,因為我想有一個好的js代碼組織結構, 我想讓自己的思路變的更加清晰。 我終于找到了--Jamal , 一個輕量級的基于jQuery的JavaScript MVC框架。
Jamal的作者Timo在Jamal剛開始的版本發布的時候,做了一個圖,搞了一個什么dispatcher, 可笑的是,國內的某些人竟然把這個圖轉來轉去的當成是寶, 呵呵,我問了Timo本人,他是這么說的:Hey Alex,
?? ?this is a slide from a presentation at the very beginning of jamal.?? ?This is not wrong in general, but there is no dispatcher anymore. The?? ?configuration, what controller is called, is now handled within the?? ?body class.
?? ?A short explanation of this structure is:?? ?- Interactions of the user are reflected in . events. So?? ?bind events to the dom within the controller.?? ?- Data is handled in the models so all XHR should go there?? ?- The view modifies the dom
?? ?This is all convention, Jamal doesn't force you to do it like this...
Greetings,?Timo
Timo這段話已經說的很清楚了,網上流傳的那張圖早就作廢了。 現在的Jamal 0.4.1支持jQuery1.3。就像Timo說的, 代碼是通過config方法和load方法來尋找controller和action。MVC簡單來說是這么約定的:
?1. C(ontroller) - 因為和用戶交互的都是.事件,所以應該在controller里和dom綁定事件。?2. M(odel) - 在models里面是進行data處理,所以會放一些ajax邏輯,以及對本地數據庫的操作(Jamal沒有實現,但是因為項目需求我會集成到里面)。?3. V(iew) - 而在view里當然是對用戶dom的修改。
當你下載了Jamal 0.4.1,里面會帶例子的。今天研究了Jamal的源碼,有一些心得:
代碼1505行:?? ?$(function(){?? ?$j = jamal = jamal();?? ?$j.start();?? ?});
這段代碼在頁面加載以后啟動Jamal。 start方法里面會調用關鍵的load方法去根據頁面body的class屬性去識別哪個controller,哪個action,所以你的頁面body標簽的class屬性必須這么寫:
class="jamal {controller:'Foos',action:'index',debug:true}"
一個頁面對應一個controller,一個controller對應一個action。 如果想像rails一樣,想寫多個action,那么可以這樣去模擬:
?? ?$j.c({Clients: {?? ? ? ? ?index: function () {?? ? ? ? ? ? ? ? ?this.new();?? ? ? ? ? ? ? ? ?// or?? ? ? ? ? ? ? ? $('.new').click(function(){?? ? ? ? ? ? ? ? $j.current.new();?? ? ? ? });?? ?},
?? ? ? ? ?new: function() {}?? ?}});
$j.c代表controller。 這樣,可以更細致的組織你的controller代碼了。如果想在Jamal里擴展自己的方法,可以這樣:
?? ?jamal.fn.extend(jamal.fn.m.prototype, ?{?? ? ? ?createTable: function(option){?? ? ? ? ? ?return true;?? ? ? ? ? // 你可以在你的model里通過重載此方法實現你具體的方法。?? ? ? ?},?? ?});
最后看看我的代碼組織目錄:
Jamal的作者Timo在Jamal剛開始的版本發布的時候,做了一個圖,搞了一個什么dispatcher, 可笑的是,國內的某些人竟然把這個圖轉來轉去的當成是寶, 呵呵,我問了Timo本人,他是這么說的:Hey Alex,
?? ?this is a slide from a presentation at the very beginning of jamal.?? ?This is not wrong in general, but there is no dispatcher anymore. The?? ?configuration, what controller is called, is now handled within the?? ?body class.
?? ?A short explanation of this structure is:?? ?- Interactions of the user are reflected in . events. So?? ?bind events to the dom within the controller.?? ?- Data is handled in the models so all XHR should go there?? ?- The view modifies the dom
?? ?This is all convention, Jamal doesn't force you to do it like this...
Greetings,?Timo
Timo這段話已經說的很清楚了,網上流傳的那張圖早就作廢了。 現在的Jamal 0.4.1支持jQuery1.3。就像Timo說的, 代碼是通過config方法和load方法來尋找controller和action。MVC簡單來說是這么約定的:
?1. C(ontroller) - 因為和用戶交互的都是.事件,所以應該在controller里和dom綁定事件。?2. M(odel) - 在models里面是進行data處理,所以會放一些ajax邏輯,以及對本地數據庫的操作(Jamal沒有實現,但是因為項目需求我會集成到里面)。?3. V(iew) - 而在view里當然是對用戶dom的修改。
當你下載了Jamal 0.4.1,里面會帶例子的。今天研究了Jamal的源碼,有一些心得:
代碼1505行:?? ?$(function(){?? ?$j = jamal = jamal();?? ?$j.start();?? ?});
這段代碼在頁面加載以后啟動Jamal。 start方法里面會調用關鍵的load方法去根據頁面body的class屬性去識別哪個controller,哪個action,所以你的頁面body標簽的class屬性必須這么寫:
class="jamal {controller:'Foos',action:'index',debug:true}"
一個頁面對應一個controller,一個controller對應一個action。 如果想像rails一樣,想寫多個action,那么可以這樣去模擬:
?? ?$j.c({Clients: {?? ? ? ? ?index: function () {?? ? ? ? ? ? ? ? ?this.new();?? ? ? ? ? ? ? ? ?// or?? ? ? ? ? ? ? ? $('.new').click(function(){?? ? ? ? ? ? ? ? $j.current.new();?? ? ? ? });?? ?},
?? ? ? ? ?new: function() {}?? ?}});
$j.c代表controller。 這樣,可以更細致的組織你的controller代碼了。如果想在Jamal里擴展自己的方法,可以這樣:
?? ?jamal.fn.extend(jamal.fn.m.prototype, ?{?? ? ? ?createTable: function(option){?? ? ? ? ? ?return true;?? ? ? ? ? // 你可以在你的model里通過重載此方法實現你具體的方法。?? ? ? ?},?? ?});
最后看看我的代碼組織目錄:
轉載于:https://blog.51cto.com/blackanger/139829
總結
以上是生活随笔為你收集整理的JavaScript MVC之Jamal的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 优化ASP.NET应用性能之ViewSt
- 下一篇: [转]文件浏览直接显示[兼容IE,Fir