0

我有一个集合数组(coll_array)。所有集合都绑定到所有事件的相同函数 (process_coll)。这意味着,对数组中任何集合的任何更改都会导致执行相同的函数。我的问题是如何识别发生事件的集合。如果我可以将参数传递给目标函数,我可以传递集合的标识,但据我所知,在 Backbone 事件中没有办法做到这一点。

initialize: function(){
    _(this).bindAll('process_coll');
    coll_array ; //array of collections 
    for(var i=0;i<coll_array.length;i++)
        coll_array[i].bind('all', this.process_coll);
        coll_array[i].fetch();
}

process_coll: function(){
    //some code here 
    //how do I get the specific collection which resulted in execution of this function?
}
4

1 回答 1

1

您最好听听特定的事件

initialize: function(){
    coll_array ; //array of collections 
    for(var i=0;i<coll_array.length;i++)
        coll_array[i].bind('reset', this.reset_coll);
        coll_array[i].bind('add', this.add_coll);
        coll_array[i].bind('remove', this.remove_coll);
        coll_array[i].fetch();
}

reset_coll: function(collection, options){
    // collection argument is the one you want
}
add_coll: function(model, collection, options){
    // collection argument is the one you want
}

remove_coll: function(model, collection, options){
    // collection argument is the one you want
}
于 2013-02-18T14:11:12.283 回答