我有以下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