fortuneRice
fortuneRice

Reputation: 4204

backbone.js passing extra data along with events

Using backbone.js, when a collection's remove method is called a "remove" event is fired.

  1. How can I extend this "remove" event to pass extra data, such as certain attributes of the particular model being removed?

  2. How can I bind to the "remove" event triggered by a specific model, specified by id or cid?

I suppose any solution would also be applicable for the "change" event? Thanks for help.

Upvotes: 0

Views: 653

Answers (1)

ryanmarc
ryanmarc

Reputation: 1680

If you're removing a model from a collection, you shouldn't need that model anymore. I guess I'm missing the point of extending remove to do more than just remove something.

When you call remove on a collection, you pass the model or an array of models in the collection to the remove function. I would recommend doing any last minute work you need with those models just before you call the remove function on your collection. At that point you should have all the models and their attributes that you plan on removing.

To bind to the change event of a specific model you just need to get the model you want from the collection and bind to that:

var myModel = myCollection.get(id); //using the id of the model

or

var myModel = myCollection.getByCid(cid); //using the cid of the model

Now bind to that model:

myModel.bind("change", function() {
    //do something
});

or, bind change to all the models in a collection:

myCollection.bind("change", function(model) {
    //do something, model is the model that triggered the change event
});

Upvotes: 2

Related Questions