forked from misteroneill/backbone-model-factory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackbone-model-factory.js
191 lines (150 loc) · 6.07 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
/*!
Backbone Model Factory 1.1.0
(c) 2012 Patrick G. O'Neill
Backbone Model Factory may be freely distributed under the MIT license
https://github.com/misteroneill/backbone-model-factory
*/
(function (root, factory) {
'use strict';
// CommonJS/NodeJS
if (typeof exports !== 'undefined' && typeof module !== 'undefined' && module.exports) {
exports = module.exports = require('backbone');
factory(exports);
// AMD/RequireJS
} else if (typeof define === 'function' && define.amd) {
define(['backbone'], factory);
// Browser global object
} else {
factory(root.Backbone);
}
})(this, function (Backbone) {
'use strict';
/**
A function which is bound to the first change of a model's idAttribute
attribute value. Note that this function is optimistic and will allow
_any_ value to be set - it does not want to assume anything about what sort
of value to store as the idAttribute attribute.
The only limitation is that this method will throw an error if you are
supplying an ID which already exists, for example:
var Model = Backbone.ModelFactory();
var a = new Model({id: 1});
var b = new Model(); // (a === b) === false
b.set('id', 1); // throws error
The reason for this behavior is that there is no way to change the object
referenced by `b` from a change listener!
@author Pat O'Neill <[email protected]>
@private
@memberof Backbone.ModelFactory
@param {Object} model
The model that changed.
@param {Mixed} value
The new value of the idAttribute attribute.
*/
function checkId(model, value) {
var key = ''+value;
var cache = model.constructor.cache;
// Backward-compatibility with Backbone pre-0.9.9
if (!model.once) {
model.off('change:' + model.idAttribute, checkId);
}
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 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.
@author Pat O'Neill <[email protected]>
@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) {
prototype = typeof Base === 'object' ? Base : (typeof prototype === 'object' ? prototype : null);
var BaseConstructor = typeof Base === 'function' ?
// Support both models generated with ModelFactory or via normal means.
(Base.hasOwnProperty('Model') ? Base.Model : Base) :
Backbone.Model;
var Model = BaseConstructor.extend(prototype);
var cache = {};
/**
Return a factory function which is treated like a constructor, but
really defers back to Model and creates instances only as needed.
@author Pat O'Neill <[email protected]>
@param {Object} [attrs]
*/
function Constructor(attrs) {
attrs = typeof attrs === 'object' ? attrs : null;
var idAttribute = Model.prototype.idAttribute;
var hasId = attrs !== null && attrs.hasOwnProperty(idAttribute);
var key = hasId && ''+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);
// If there is no match in the cache, store the new model.
if (!exists) {
cache[key] = model;
// If there is a match in the cache, update its attributes based on the
// passed-in attributes.
} else {
model.set(attrs);
}
// If no value for the idAttribute attribute was supplied, add a check
// for when the model gets one.
if (!hasId) {
// Backward-compatibility with Backbone pre-0.9.9
model[model.once ? 'once' : 'on']('change:' + idAttribute, checkId);
}
// 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 public reference to the actual model on the constructor. Use this for
instanceof checks.
@author Pat O'Neill <[email protected]>
@example
var Foo = Backbone.ModelFactory();
var foo = new Foo();
console.log(foo instanceof Foo.Model); // true
*/
Constructor.Model = Model;
/**
A public reference to the cache of models generated by this constructor.
Since garbage collection will NOT clear model instances created by model
factory constructors, it may be useful to delete unneeded models from this
object directly.
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 will (i.e. newly created
models) will not be stored in cache until they gain a value!
@author Pat O'Neill <[email protected]>
@example
var Foo = Backbone.ModelFactory();
var foo = new Foo({id: 1});
delete Foo.Model.cache['1'];
*/
Constructor.Model.cache = cache;
return Constructor;
};
return Backbone;
});