forked from ahmdrefat/backbone.uniquemodel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backbone.uniquemodel.js
276 lines (222 loc) · 7.66 KB
/
backbone.uniquemodel.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
/*jshint unused:true, undef:true, strict:true*/
/*global global, _, Backbone*/
(function (window) {
"use strict";
var globalCache;
/**
* UniqueModel wrapper converts regular Backbone models into
* unique ones.
*
* Example:
* var UniqueUser = UniqueModel(User);
*
* If this is model is synced between windows, you need to
* specify the model's name (string) and a valid storage adapter
* (currently just 'localStorage').
*
* Example:
* var SyncedUniqueUser = UniqueModel(User, 'User', 'localStorage');
*/
function UniqueModel(Model, modelName, storageAdapter) {
modelName = modelName || _.uniqueId('UniqueModel_');
storageAdapter = storageAdapter || UniqueModel.STORAGE_DEFAULT_ADAPTER;
var cache = UniqueModel.addModel(Model, modelName, storageAdapter);
return cache.modelConstructor;
}
UniqueModel.globalCache = globalCache = {};
UniqueModel.STORAGE_DEFAULT_ADAPTER = 'memory';
UniqueModel.STORAGE_KEY_DELIMETER = '.';
UniqueModel.STORAGE_NAMESPACE = 'UniqueModel';
// Returns the cache associated with the given Model.
UniqueModel.getModelCache = function (modelName) {
var cache = globalCache[modelName];
if (!cache)
throw "Unrecognized model: " + modelName;
return cache;
};
UniqueModel.addModel = function (Model, modelName, storageAdapter) {
// Throw error here? (added twice)
if (globalCache[modelName])
return globalCache[modelName];
var cache = new ModelCache(Model, modelName, storageAdapter);
globalCache[modelName] = cache;
return cache;
};
// Clears all in-memory instances
UniqueModel.clear = function () {
for (var modelName in globalCache) {
if (globalCache.hasOwnProperty(modelName))
delete globalCache[modelName];
}
};
/*
* Encapsulates a cache for a single model.
*/
function ModelCache (Model, modelName, storageAdapter) {
var self = this;
this.instances = {};
this.Model = Model;
this.modelName = modelName;
this.storage = null;
if (storageAdapter === 'localStorage') {
this.storage = new LocalStorageAdapter(this.modelName);
}
if (this.storage) {
this.storage.on('sync', this.storageSync, this);
this.storage.on('destroy', this.storageDestroy, this);
}
var modelConstructor = function (attrs, options) {
var id, res;
if (attrs && attrs._res) {
res = attrs._res.split('@')[0];
id = res.split('/').reverse()[0].split('.')[1];
if (id) {
attrs.id = id;
}
}
return self.get(attrs, options);
};
_.extend(modelConstructor, Backbone.Events);
// Backbone collections need prototype of wrapped class
modelConstructor.prototype = this.Model.prototype;
this.modelConstructor = modelConstructor;
}
_.extend(ModelCache.prototype, {
newModel: function (attrs, options) {
var instance = new this.Model(attrs, options);
if (this.storage) {
if (instance.id)
this.storage.save(instance.id, instance.attributes);
instance.on('sync', this.instanceSync, this);
instance.on('destroy', this.instanceDestroy, this);
}
return instance;
},
// Event handler when 'sync' is triggered on an instance
instanceSync: function (instance) {
if (this.storage)
this.storage.save(instance.id, instance.attributes);
},
// Event handler when 'destroy' is triggered on an instance
instanceDestroy: function (instance) {
if (this.storage)
this.storage.remove(instance.id);
},
// Event handler when 'sync' is triggered on the storage adapter
storageSync: function (id, attrs) {
this.get(attrs, { fromStorage: true });
},
// Event handler when 'destroy' is triggered on the storage handler
storageDestroy: function (id) {
var instance = this.instances[id];
if (instance) {
instance.trigger('destroy', instance);
delete this.instances[id];
}
},
add: function (id, attrs, options) {
var instance = this.newModel(attrs, options);
this.instances[id] = instance;
return instance;
},
get: function (attrs, options) {
options = options || {};
var Model = this.Model;
var id = attrs && attrs[Model.prototype.idAttribute];
// If there's no ID, this model isn't being tracked; return
// a new instance
if (!id)
return this.newModel(attrs, options);
// Attempt to restore a cached instance
var instance = this.instances[id];
if (!instance) {
// If we haven't seen this instance before, start caching it
instance = this.add(id, attrs, options);
if (options.fromStorage)
this.modelConstructor.trigger('uniquemodel.add', instance);
} else {
// Otherwise update the attributes of the cached instance
instance.set(attrs);
if (!options.fromStorage)
this.instanceSync(instance);
}
return instance;
}
});
/**
* Wraps localStorage access and onstorage events. Designed
* so that this can be swapped out for another adapter (i.e.
* sessionStorage or a localStorage-backed library like lscache)
*/
function LocalStorageAdapter (modelName) {
this.modelName = modelName;
LocalStorageAdapter.instances[modelName] = this;
// Global listener - only listen once
if (!LocalStorageAdapter.listener) {
LocalStorageAdapter.listener = window.addEventListener ?
window.addEventListener('storage', LocalStorageAdapter.onStorage, false) :
window.attachEvent('onstorage', LocalStorageAdapter.onStorage);
}
}
// Hash of LocalStorageAdapter instances
LocalStorageAdapter.instances = {};
// Reference to the global onstorage handler
LocalStorageAdapter.listener = null;
LocalStorageAdapter.onStorage = function (evt) {
// TODO: IE fires onstorage even in the window that fired the
// change. Deal with that somehow.
var key = evt.key;
// This will process *all* storage events, so make sure not to choke
// on events we're not interested in.
// Example regex output: /UniqueModel\.(\w+)\.(.+)/
var re = new RegExp([
UniqueModel.STORAGE_NAMESPACE, // namespace (default is UniqueModel)
'(\\w+)', // class name
'(.+)' // key
].join('\\' + UniqueModel.STORAGE_KEY_DELIMETER));
var match = key.match(re);
if (!match)
return;
var modelName = match[1];
var id = match[2];
var adapter = LocalStorageAdapter.instances[modelName];
if (!adapter)
return;
adapter.handleStorageEvent(key, id);
};
_.extend(LocalStorageAdapter.prototype, {
handleStorageEvent: function (key, id) {
var json = localStorage.getItem(key);
if (!json)
this.trigger('destroy', id);
else
this.trigger('sync', id, JSON.parse(json));
},
getStorageKey: function (id) {
// e.g. UniqueModel.User.12345
var str = [
UniqueModel.STORAGE_NAMESPACE,
this.modelName,
id
].join(UniqueModel.STORAGE_KEY_DELIMETER);
return str;
},
save: function (id, attrs) {
if (!id)
throw 'Cannot save without id';
var json = JSON.stringify(attrs);
localStorage.setItem(this.getStorageKey(id), json);
},
remove: function (id) {
if (!id)
throw 'Cannot remove without id';
localStorage.removeItem(this.getStorageKey(id));
}
}, Backbone.Events);
// Exports
_.extend(UniqueModel, {
ModelCache: ModelCache,
LocalStorageAdapter: LocalStorageAdapter
});
window.Backbone.UniqueModel = UniqueModel;
})(typeof global === "object" ? global : this);