现附上一段简单的代码,用的Backbone库
var NoteCollectionView = Backbone.View.extend({
tagName: 'ul',
initialize: function () {
this.collection.on('add', this.addOne, this);
this.render();
},
render: function () {
this.collection.each(this.addOne, this);
return this;
},
addOne: function(note) {
var noteView = new NoteView({model: note});
this.$el.append(noteView.render().el);
}
});
var noteCollection = new NoteCollection([note1, note2, note3]);
var noteCollectionView = new NoteCollectionView({collection: noteCollection});
有一个小问题:
initialize: function () {
this.collection.on('add', this.addOne, this);
this.render();
},
render: function () {
this.collection.each(this.addOne, this);
return this;
},
加粗那两个this代表什么意思呢?搞不明白
怎么取得上下文的,困扰了我好久了