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?
}
_(this).bindAll('process_coll')對我來說看起來並不優雅。也許你需要一個不同的設計?請參閱http://backbonejs.org/#Events-trigger。觸發事件時可以傳遞參數。 – Pramod 2013-02-18 08:39:12
集合將被傳遞給參數列表中的處理程序。當然,參數順序取決於事件,因此您可以將其分別綁定到各個事件(如Paul所示),也可以嘗試解析'arguments'來查找集合。 – 2013-02-18 18:15:47