I have a collection where is has an event that gets fired when a model is added. I have read in the docs where it should have an options parameter but not able to get to it. I basically want to find the index the model is at in the collection. Inside my collection I have this.
initialize: function( ) {
this.bind( 'add', this.onModelAddedd, this );
},
onModelAddedd: function( model, options ){
console.log("options = ", options);
}
Backbone. Backbone has been around for a long time, but it's still under steady and regular development. It's a good choice if you want a flexible JavaScript framework with a simple model for representing data and getting it into views.
Backbone. js is a model view controller (MVC) Web application framework that provides structure to JavaScript-heavy applications. This is done by supplying models with custom events and key-value binding, views using declarative event handling and collections with a rich application programming interface (API).
The documentation is a little unclear on this so your confusion is understandable. From the fine manual:
Catalog of Events
Here's a list of all of the built-in events that Backbone.js can fire. You're also free to trigger your own events on Models and Views as you see fit.
- "add" (model, collection, options) — when a model is added to a collection.
- ...
So the second argument to the add
handler is the collection itself. The ubiquitous options
that you're looking for is always the last argument so you want this:
onModelAddedd: function(model, collection, options) {
console.log("options = ", options);
}
Demo (open your console please): http://jsfiddle.net/ambiguous/Das2t/
The final options
argument is implied to be the last argument throughout the documentation but it isn't explicitly spelled out anywhere.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With