1
我正在关注位于here的BackboneJs上的教程。BackboneJs教程问题
关于在render
方法,他做了以下中途下:
events: { 'click button#add': 'addItem' },
initialize: function() {
this.collection = new List();
// Collection event binder
this.collection.bind('add', this.appendItem);
this.counter = 0;
this.render();
},
render: function() {
this.el.append("<button id='add'> Add List Item</button>");
this.el.append("<ul></ul>");
_(this.collection.models).each(function(item){
// in case collection is not empty
appendItem(item);
}, this);
},
addItem: function() {
var item = new Item();
this.counter++;
item.set({
part2: item.get('part2') + " " + this.counter
});
this.collection.add(item);
},
appendItem: function (item) {
$('ul').append('<li>' + item.get('part1') + " " + item.get('part2') + '</li>');
}
我有几个关于线下的问题。
_(this.collection.models).each(function(item){
// in case collection is not empty
appendItem(item);
}, this);
下划线_
在这方面做了什么?
为什么这甚至需要?
如果收集不是空的,则说明注释。但是,没有这条线它工作得很好。 initialize
函数中的bind
覆盖告诉Backbone在集合上触发add
事件时运行this.appendItem
,或者我认为并通过删除相关行来确认。
完美。谢谢。我完全忘记了下划线是一个依赖:)我认为这可能是一个主干的事情。 –