最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Overriding Backbone.Collection.prototype.add - Stack Overflow

programmeradmin2浏览0评论

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

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

Share asked Jul 18, 2012 at 18:23 luisgoluisgo 2,4754 gold badges25 silver badges30 bronze badges 2
  • Note: var = newModels = models.items; should be var newModels = models.items;. I'm aware of it but that is not what is breaking the code. – luisgo Commented Jul 18, 2012 at 18:27
  • Ever get this resolved? – seebiscuit Commented May 12, 2014 at 15:36
Add a ment  | 

1 Answer 1

Reset to default 8

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.

发布评论

评论列表(0)

  1. 暂无评论