【发布时间】:2014-01-10 20:40:35
【问题描述】:
我已将实例存储添加到我的主干模型中。当我手动创建一个带有 id 的对象时,它可以工作并返回一个新的或已经存在的模型。我如何将此功能也集成到主干集合中。 你能给我一个提示,我必须重写哪些方法/方法?
我的实例存储代码如下所示:
define(function(require) {
var Backbone = require('backbone');
return Backbone.Model.extend({
constructor: function(attributes, options) {
var id = attributes ? attributes.id : undefined;
if(this.store[id])
return this.store[id];
Backbone.Model.prototype.constructor.apply(this, arguments);
if(id)
this.store[id] = this;
this.count[id] = this.count[id] ? this.count[id] + 1 : 1;
}
});
});
感谢任何想法或提示!
【问题讨论】:
标签: javascript backbone.js backbone.js-collections