Adding a Database

I have a collection where there is an event that fires when a model is added. I read in the docs where it should have the options parameter, but it won't be able to get it. I basically want to find the index where the model is in the collection. In my collection, I have this.

initialize: function( ) { this.bind( 'add', this.onModelAddedd, this ); }, onModelAddedd: function( model, options ){ console.log("options = ", options); } 
+8
source share
1 answer

The documentation is a bit unclear, so your confusion is understandable. From a great guide :

Event catalog

Here is a list of all the built-in events that can trigger Backbone.js. You can also trigger your own events on models and views as you wish.

  • "add" (model, collection, parameters) - when adding a model to the collection.
  • ...

So, the second argument to the add handler is the collection itself. The ubiquitous options you are looking for is always the last argument, so you want:

 onModelAddedd: function(model, collection, options) { console.log("options = ", options); } 

Demo (open console): http://jsfiddle.net/ambiguous/Das2t/

The last argument to options implied as the last argument in the entire documentation, but it is not explicitly specified elsewhere.

+23
source share

All Articles