This repository has been archived by the owner on Sep 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbackbone-model-factory.js
280 lines (247 loc) · 9 KB
/
backbone-model-factory.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/* global define */
(function (root, factory) {
'use strict';
// CommonJS/NodeJS
if (typeof exports !== 'undefined' && typeof module !== 'undefined' && module.exports) {
exports = module.exports = require('backbone');
factory(require('underscore'), exports);
// AMD/RequireJS
} else if (typeof define === 'function' && define.amd) {
define(['underscore', 'backbone'], factory);
// Browser global object
} else {
factory(root._, root.Backbone);
}
})(this, function (_, Backbone) {
'use strict';
/**
* Gets the model's `idAttribute` value as a string.
*
* @private
* @param {Object} model
* @return {String}
*/
function getId(model) {
return String(model.get(model.idAttribute));
}
/**
* This method on the `Backbone` object is used to generate model
* constructors which will enforce the existence of only a single object
* with any given value for the `idAttribute` attribute.
*
* @example
* var Foo = Backbone.ModelFactory();
* var foo1 = new Foo({id: 1});
* var foo2 = new Foo({id: 1});
* var foo3 = new Foo({id: 2});
*
* console.log(foo1 === foo2); // true
* console.log(foo1 === foo3); // false
*
* @param {Function|Object} Base
* The model constructor which the new model constructor should
* extend. If a non-function object is given here, the new model
* constructor will extend a and this argument will be applied
* as the `prototype` instead.
*
* @param {Object} prototype
* If a `Base` model constructor is given, this argument should be
* the prototype object of the new model constructor.
*
* @return {Function} A model constructor.
*/
Backbone.ModelFactory = function (Base, prototype) {
var cache = {};
/**
* Fires an appropriate constructor wipe event given a constructor
* and an array of models.
*
* @private
* @param {Array} models
*/
var constructorWipeEvent = function (models) {
var remainder = _.values(cache);
var events = 'wipe wipe:' + (remainder.length ? 'some' : 'all');
Constructor.trigger(events, Constructor, models, remainder);
};
/**
* A factory function which is treated like a constructor, but really
* defers back to Model and creates instances only as needed.
*
* @param {Object} attrs
* @param {Object} options
*/
var Constructor = function (attrs, options) {
attrs = _.isObject(attrs) ? attrs : null;
var idAttribute = Model.prototype.idAttribute;
var hasId = attrs !== null && attrs.hasOwnProperty(idAttribute);
var key = hasId && String(attrs[idAttribute]);
var exists = key && cache.hasOwnProperty(key);
// Use any cached model or instantiate a new one.
var model = exists ? cache[key] : new Model(attrs, options);
model.constructor = Constructor;
// If there is no match in the cache, store the new model.
if (key && !exists) {
cache[key] = model;
// If there is a match in the cache, update its attributes based on the
// passed-in attributes.
} else {
if (options && options.parse) {
attrs = model.parse(attrs, options);
}
model.set(attrs, options);
}
// If no value for the idAttribute was supplied, add a check for when the
// model gets one.
if (!hasId) {
model.once('change:' + idAttribute, function (model, value) {
var key = getId(model);
if (cache.hasOwnProperty(key)) {
// This should not happen unless you're doing something really strange
// with your `idAttribute` attribute values!
throw new Error('model idAttribute attribute value already exists in cache');
} else {
cache[key] = model;
}
});
}
// This is the magic part - if a constructor returns an object, a new
// object is NOT generated (though we may generate one internally).
return model;
};
/**
* A reference to the actual model on the constructor.
*
* @property {Function} _Model
* @example
* var Foo = Backbone.ModelFactory();
* var foo = new Foo();
*
* console.log(foo instanceof Foo.Model); // true
*/
var Model = Constructor._Model = (function (B, p) {
var baseIsFunc = typeof B === 'function';
p = _.extend({
/**
* Removes a model instance from its constructor's cache. Will exist on
* the `prototype` chain of all model instances generated from
* `Backbone.ModelFactory` constructors.
*
* @method wipe
* @param {Object} [options]
* @param {Boolean} [options.silent=false]
* If `true`, suppresses triggering of all event(s).
* @param {Boolean} [options._suppress=false]
* Intended to be for internal use, if `true`, suppresses
* triggering of constructor-level event(s). `silent` takes
* precedence over this option.
*/
wipe: function (options) {
options = options || {};
delete cache[getId(this)];
if (!options.silent) {
this.trigger('wipe', this);
if (!options._suppress) {
constructorWipeEvent([this]);
}
}
}
}, baseIsFunc ? p : B);
// Support both models generated with ModelFactory or via normal means.
B = baseIsFunc ? (B._Model || B) : Backbone.Model;
return B.extend(p);
})(Base, prototype);
/**
* Since garbage collection will _not_ clear model instances created by
* model factory constructors, it may be useful to uncache model
* instances which are no longer needed from this object directly - or
* use the `wipe` method on the constructor or the model itself.
*
* Model instances are stored under the value of their `idAttribute` -
* for example, by default they will be stored using the value of their
* `id` attribute. Models without an `idAttribute` value (i.e. newly
* created models) will not be stored in cache until they gain an
* `idAttribute` value.
*
* @property {Object} _cache
* @example
* var Foo = Backbone.ModelFactory();
* var foo = new Foo({id: 1});
*
* Foo.wipe(foo);
*
* var bar = new Foo({id: 1});
* console.log(bar === foo); // false
* bar.wipe();
*
* var baz = new Foo();
* baz.set({id: 2});
* console.log(new Foo({id: 2}) === baz); // true
*/
Constructor._cache = cache;
/**
* The `Constructor` and `Constructor.Model` share `prototype`s so that
* `instanceof` checks can support either function.
*
* @property {Object} prototype
* @example
* var Foo = Backbone.ModelFactory();
* var foo = new Foo();
*
* console.log(foo instanceof Foo.Model); // true
* console.log(foo instanceof Foo); // true
*/
Constructor.prototype = Model.prototype;
/**
* Remove a model instance, a subset of instances, or _all_ instances from
* this constructor's cache.
*
* @method wipe
* @param {Object|Backbone.Collection|Array|undefined} models
* Pass a single model instance (of this constructor only!), an
* array of instances, or a collection to wipe only those
* instances from the cache.
*
* Pass nothing (or any `falsy` value) to wipe _all_ models
* from the cache.
*
* @param {Object} [options]
* @param {Boolean} [options.silent=false]
*/
Constructor.wipe = function (models, options) {
options = options || {};
// Handle case of a collection.
if (models instanceof Backbone.Collection) {
models = [].concat(models.models);
// Handle case of a full wipe.
} else if (!models) {
models = _.values(this._cache);
// Handle case of an arbitrary value (presumably a model instance, but
// the filter will take care of it if not).
} else if (!_.isArray(models)) {
models = [models];
}
// At this point, if we have an empty array, it likely means either the
// collection or array was empty. No action is taken.
if (!models.length) {
return;
}
// Filter down to model instances of this specific model only!
models = _.filter(models, function (model) {
return model instanceof this;
}, this);
// Finally, wipe all models still in the array - triggering a single
// event for each - and trigger the appropriate event on the constructor.
if (models.length) {
_.invoke(models, 'wipe', _.extend({_suppress: true}, options));
constructorWipeEvent(models);
} else {
throw new Error('invalid argument');
}
};
// Make the constructor into an event bus.
_.extend(Constructor, Backbone.Events);
return Constructor;
};
return Backbone;
});