Override Backbone.Collection.prototype.add

Is it possible to override the collection.add method globally in the spine like this:

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);
}

Api, which I use ALWAYS, contains actual models on one level for collections. In itemsand I believe that overrides the method .addfor all collections. I tried what I had above, but it didn't seem to work. Any ideas?

Thank,

Louis

+5
source share
1 answer

Try the following:

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

Then you can expand all collections from Example.

+8
source

All Articles