Backbone 及依賴庫
- underscore
- zepto/jquery
Backbone 的特性
- 輕量級
- MVC結(jié)構(gòu)化
- 繼承機(jī)制
- 建立與服務(wù)器的無縫鏈接
- 界面事件管理
- 事件可以是任意事件
- 元素可以是滿足jquery的選擇器
- 后邊是響應(yīng)方法
- 輕量級模板解析
- 自定義事件管理
model.on('selfEvent',function(){})
model.off('selfEvent')
model.trigger('selfEvent',val1,val2...)
- 路由器
// 在 view 中
events: {
'click [role=edit]':'add'
},
add: function () {}
// 在 route 中
var CustomRouter = Backbone.Router.extend({
routes : {
'' : 'index', // 當(dāng)URL Hash在根目錄時執(zhí)行index方法:url#
'list' : 'getList', // 當(dāng)URL Hash在list節(jié)點(diǎn)時執(zhí)行g(shù)etList方法:url#list
'detail/:id' : 'query', // 當(dāng)URL Hash在detail節(jié)點(diǎn)時執(zhí)行query方法,并將detail后的數(shù)據(jù)作為參數(shù)傳遞給query方法:url#list/1001
'*error' : 'showError' // 當(dāng)URL Hash不匹配以上規(guī)則時, 執(zhí)行error方法
},
index : function() {
alert('index');
},
getList : function() {
alert('getList');
},
query : function(id) {
alert('query id: ' + id);
},
showError : function(error) {
alert('error hash: ' + error);
},
});
var custom = new CustomRouter();
Backbone.history.start();
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。