我有以下Backbone.js的模型和收集:爲什麼沒有骨幹視圖檢測模型更改?
// View
lr.MapView = Backbone.View.extend({
el: $('#wrapper'),
initialize: function() {
this.date = this.options.date;
_.bindAll(this, "render", "addAllEvents", "addOneEvent", "showNextDay", "collectData");
this.collectData();
},
events: {
"click #next": "showNextDay",
},
collectData: function() {
var that = this;
if (this.collection) this.collection.reset();
this.collection = new lr.Events([], { date : this.date });
this.collection.fetch({
success: function(resp) {
that.render();
that.addAllEvents();
}
});
},
showNextDay: function() {
this.date = this.date.add(1).days();
this.collectData();
},
addAllEvents: function() {
this.collection.each(this.addOneEvent);
},
addOneEvent: function(e) {
var ev = new lr.EventView({
model: e,
parentView: this
});
},
...
});
// Sub-view
lr.EventView = Backbone.View.extend({
initialize: function() {
var that = this;
this.model.bind('change', function() {
console.log('foo');
that.remove();
});
}
...
});
我試圖子視圖改變綁定:這與視圖及其子視圖中使用
// Model
lr.Event = Backbone.Model.extend({});
// Collection
lr.Events = Backbone.Collection.extend({
model: lr.Event,
initialize: function(models, options) {
this.date = options.date;
},
url: function() {
return '/events' + '?date=' + this.date.toString('yyyy-MM-dd');
}
});
到其相關模型。所以我希望當我打電話給this.collection.reset()
時,console.log('foo')
會被調用。但事實並非如此。我之前和之後編輯了集合,我重置它之後,它肯定會從「某事到無」,所以我假設我在某種程度上搞亂了子視圖與模型的綁定。
任何人都看到錯誤?
完整意義。謝謝! – AdamVickers 2012-02-15 00:27:47