我正在尝试制作一个简单的视图模型来显示带有击退的对象列表。View Model 相当简单,如下所示:
var objectives_collection = new ObjectiveCollection();
objectives_collection.fetch();
var view_model = kb.ViewModel.extend({
objectives: kb.CollectionObservable(objectives_collection),
constructor: function() {
_this = this;
kb.ViewModel.prototype.constructor.call(this, model = new Objective(), {});
console.log(this.objectives);
return this;
},
addObjective : function() {
var objective_model = new Objective({name: "New Objective", descriptor: 'Add description here'});
this.objectives.add(objective_model);
objective_model.save();
console.log(objectives);
},
edit: function() {
console.log('Edit');
},
remove: function(objective) {
self.objectives.remove(objective);
}
});
不过现在,视图模型的第一行 (kb.CollectionObservable(objectives_collection)) 让我很伤心。它正在调用 Underscore.js 的绑定函数并尝试设置 onCollectionChange 侦听器,但此处的 Knockback 行正在尝试绑定未定义的函数:
this.__kb._onCollectionChange = _.bind(this._onCollectionChange, this);
this._onCollectionChange 显然是未定义的。我不确定该怎么做。我在这里做错了什么?