Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Backbone add event

Tags:

backbone.js

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);
    }
like image 352
Chapsterj Avatar asked Mar 26 '12 15:03

Chapsterj


People also ask

Does anyone use Backbone JS?

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.

What is Backbone in programming?

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).


1 Answers

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.

like image 109
mu is too short Avatar answered Jan 23 '23 11:01

mu is too short