我是 Backbone.js 的新手,从 JS 开发的“标准”模型中出来的人我有点不确定如何使用这些模型(或何时)。
视图看起来非常明显,因为它模拟了大多数 JS 开发人员熟悉的典型“监听事件并做某事”方法。
我构建了一个简单的待办事项列表应用程序,到目前为止还没有看到需要这个model
方面,所以我很好奇是否有人可以给我一些关于如何将它应用到这个应用程序的见解,或者它是否可以发挥作用如果我正在处理更复杂的数据。
这是JS:
Todos = (function(){
var TodoModel = Backbone.Model.extend({
defaults: {
content: null
}
});
var TodoView = Backbone.View.extend({
el: $('#todos'),
newitem: $('#new-item input'),
noitems: $('#no-items'),
initialize: function(){
this.el = $(this.el);
},
events: {
'submit #new-item': 'addItem',
'click .remove-item': 'removeItem'
},
template: $('#item-template').html(),
addItem: function(e) {
e.preventDefault();
this.noitems.remove();
var templ = _.template(this.template);
this.el.append(templ({content: this.newitem.val()}));
this.newitem.val('').focus();
return this;
},
removeItem: function(e){
$(e.target).parent('.item-wrap').remove();
}
});
self = {};
self.start = function(){
new TodoView();
};
return self;
});
$(function(){
new Todos(jQuery).start();
});
在这里运行:http: //sandbox.fluidbyte.org/bb-todo