Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/387.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 主干模型(集合)更改事件未触发_Javascript_Backbone.js - Fatal编程技术网

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)

谢谢是的,变量命名有误导性,但我只是在玩弄主干。。