Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overriding Backbone.Collection.prototype.add

Is it possible to override the collection.add method globally in backbone like so:

Backbone.Collection.prototype._add = Backbone.Collection.prototype.add;
Backbone.Collection.prototype.add = function(models, options) {
    var = newModels = models.items;
    Backbone.Collection.prototype._add(newModels, options);
}

The api I'm using ALWAYS contains the actual models one level down for collections. Under items and I find myself overriding the .add method for all collections. I tried what I have above but it didn't seem to work. Any ideas?

Thanks,

Luis

like image 921
luisgo Avatar asked Jul 18 '12 18:07

luisgo


1 Answers

Try the following:

var Example = Backbone.Collection.extend({
    add: function(models, options) {
        Backbone.Collection.prototype.add.call(this, models.items, options);
    }
})

Then you can extend all collections from Example.

like image 174
imsky Avatar answered Oct 15 '22 15:10

imsky