Search code examples
javascriptbackbone.jsbackbone-events

Last element in Backbone collection add / reset?


How do you know if a model is the last one in the batch?

   // inside view    
collection = new ObjectCollection();
this.listenTo(collection, 'add', added);

added: function(mod){
    //if(mod.position.last)
}

collection.add([{'name': 'Human'}, {'name': 'Cat'}]);

Solution

  • http://backbonejs.org/#Collection-at

    added: function(mod){
        if (this.collection.at(this.collection.length-1) == mod) {
          //do stuff
        }
    }