shreyj
shreyj

Reputation: 1807

Backbone.js: Binding an array of collection to a function

I have an array of collections (coll_array). All collections are bound to same function (process_coll) on all events. That means, any change to any of collection in the array results in execution of same function. My problem is how do I identify the collection on which the event took place. If I could pass arguments to the target function I could pass the identity of the collection but as far as I know there is no way to do it in Backbone events.

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

Upvotes: 0

Views: 193

Answers (1)

Paul Hoenecke
Paul Hoenecke

Reputation: 5060

You are probably better off listening for 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
}

Upvotes: 1

Related Questions