可能重复:
Backbone.js - change not triggering while the name change
我创建了一个学习骨干的函数...因为我在本地创建了一个'json'并在我的fetch函数中调用它。我每隔10秒就一直调用fetch函数。除此之外,在我之间我手动更新我的'本地json',在更改后fetch
应该让我更新的东西,但它没有让我。
我还添加了一个触发函数,而名称attribute
发生变化但是当josn更新名称时不会触发
这两个都令我感到困惑我也没有得到一个很好的教程来学习这个...任何人帮我解决这个问题或建议我找到两者的正确方法?
代码:
(function($){
var list = {};
list.model = Backbone.Model.extend({
defaults:{
name:'need the name'
}
});
list.collect = Backbone.Collection.extend({
model:list.model,
url : 'data/names.json',
initialize:function(){
this.fetch();
this.keepUpdate();
},
keepUpdate:function(){
var that = this;
var updateData = function(){
that.fetch();
myTimeout = setTimeout(updateData,10000);
}
var myTimeout = setTimeout(updateData,10000);
}
});
list.view = Backbone.View.extend({
initialize:function(){
this.collection = new list.collect();
this.collection.on("reset", this.render, this);
this.collection.on('change:name',function(){ console.log('name changed')}); // not triggers while i change the name attrb..
},
render:function(){
_.each(this.collection.models, function(data){
console.log(data.get('name')); // no update getting here..
})
}
});
var newView = new list.view();
})(jQuery)