【发布时间】:2012-10-25 13:02:18
【问题描述】:
我是 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();
});
【问题讨论】:
标签: javascript backbone.js model