-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModelRegistry.js
64 lines (56 loc) · 2.1 KB
/
ModelRegistry.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
define(function (require) {
var log = function () {
console.log.call(console, '[Registry] ' + m, Array.prototype.slice.call(arguments, 1));
}
// Model Registry
// Here we store all models reference so they don't have to be duplicated,
// based on the idAttribute
return {
_instances:{},
// Adds a new model to the registry
add:function (model) {
var self = this
// Initialize the Model's registry
if (!this._instances[model.name]) this._instances[model.name] = {};
// Add this model to the registry immediately
if (model.id) {
this._set(model.name, model.id, model);
}
// Wait until we have an id
else {
var waitForId = function () {
self._set(model.name, model.id, model);
model.off('change:_id', waitForId);
};
model.on('change:_id', waitForId);
}
// Wait for its destruction for removal from registry
var waitForDestroy = function () {
self.remove(model);
model.off('destroy', waitForDestroy); // just to be sure we do our part
}
model.on('destroy', waitForDestroy);
},
// Simply sets the new registry model
_set:function (modelName, id, model) {
if (this._instances[modelName][id]) {
log('Overwriting model', modelName, id, model);
}
this._instances[modelName][id] = model;
},
// Removes a model or an id from the registry
remove:function (modelName, id) {
if (!id) {
id = modelName.id;
modelName = modelName.name;
}
if (id && this._instances[modelName]) {
delete this._instances[modelName][id];
}
},
// Returns a model from the registry
fetch:function (modelName, id) {
return this._instances[modelName] && this._instances[modelName][id];
}
}
});