2013-02-18 93 views
0

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

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? 
} 
+0

_(this).bindAll('process_coll')对我来说看起来并不优雅。也许你需要一个不同的设计?请参阅http://backbonejs.org/#Events-trigger。触发事件时可以传递参数。 – Pramod 2013-02-18 08:39:12

+0

集合将被传递给参数列表中的处理程序。当然,参数顺序取决于事件,因此您可以将其分别绑定到各个事件(如Paul所示),也可以尝试解析'arguments'来查找集合。 – 2013-02-18 18:15:47

回答

1

您可能最好是倾听specific events

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 
} 
相关问题