Javascript 主干模型(集合)更改事件未触发
有人能向我解释为什么我的代码中从不触发更改事件吗?以前也有人问过类似的问题,但对我没有帮助。我想我的代码中有一个我没有看到的普遍错误Javascript 主干模型(集合)更改事件未触发,javascript,backbone.js,Javascript,Backbone.js,有人能向我解释为什么我的代码中从不触发更改事件吗?以前也有人问过类似的问题,但对我没有帮助。我想我的代码中有一个我没有看到的普遍错误 item = Backbone.Model.extend({}); itemCollection = Backbone.Collection.extend({model:item, url:"/get.json/data?query=getcol"}); search=Backbone.View.extend({ el: $("#searchView")
item = Backbone.Model.extend({});
itemCollection = Backbone.Collection.extend({model:item, url:"/get.json/data?query=getcol"});
search=Backbone.View.extend({
el: $("#searchView"),
events: {
'keypress #search_query' : 'lookup',
'click #search_button' : 'lookup',
},
initialize: function() {
this.model = new itemCollection;
this.model.on("change", this.render, this);
},
render: function() {
now=new Date().getTime(); /*used to make render() calls visible*/
json = JSON.stringify(this.model);
$("#searchResults").html(json + " " + now);
},
lookup: function() {
this.model.url = "/get.json/data?query=" + $("#search_query").val();
this.model.fetch();
}
});
searchview = new search;
(路径“/get.json/data?query=”正确。)获取集合时,会触发
sync
事件。所以改变
model.on(“change”,this.render,this)
到
model.on(“sync”,this.render,this)
为了清晰起见,最好在代码中将this.model
重命名为this.collection
。还考虑使用事件,以便在删除视图时清理事件。
this.listenTo(this.collection, "sync", this.render, this)
谢谢是的,变量命名有误导性,但我只是在玩弄主干。。