forked from disqus/backbone.uniquemodel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackbone.uniquemodel.js
323 lines (257 loc) · 9.36 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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/*jshint unused:true, undef:true, strict:true*/
/*global global, _, Backbone*/
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('underscore'), require('backbone')) :
typeof define === 'function' && define.amd ? define(['underscore', 'backbone'], factory) :
(global.UniqueModel = global.Backbone.UniqueModel = factory(global._,global.Backbone));
}(this, function (_,Backbone) { 'use strict';
_ = 'default' in _ ? _['default'] : _;
Backbone = 'default' in Backbone ? Backbone['default'] : Backbone;
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.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 StorageAdapter(this.modelName, localStorage);
} else if (storageAdapter === 'sessionStorage') {
this.storage = new StorageAdapter(this.modelName, sessionStorage);
}
if (this.storage) {
this.storage.on('sync', this.storageSync, this);
this.storage.on('destroy', this.storageDestroy, this);
}
var modelConstructor = function (attrs, options) {
return self.get(attrs, options);
};
// Extend Model's static properties onto new
_.extend(modelConstructor, Model);
// NOTE: currently possible for Backbone.Events functions to collide with
// Model static properties e.g. Model.on vs Backbone.Events.on
_.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 (!instance.id) {
// If this model currently has no id, but gains one later (e.g. via sync),
// then add it to the list of tracked instances
instance.once('change:' + instance.idAttribute, function () {
if (!this.instances[instance.id])
this.instances[instance.id] = instance;
}, this);
}
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) {
var id = instance.id;
if (this.storage)
this.storage.remove(id);
// Stop tracking this model; otherwise mem leak (there are other
// sources of memory leaks we need to address, but hey, here's one)
if (this.instances[id])
delete this.instances[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 locally cached instance
var instance = this.instances[id];
// Attempt to restore a cached instance from storage
if(this.storage &&
// if this wasn't from a storage event
!options.fromStorage &&
// and there isn't already an existing instance
!instance
) {
var instanceAttrs = this.storage.getFromStorage(this.storage.getStorageKey(id));
if (instanceAttrs)
instance = this.add(id, instanceAttrs, options);
}
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 StorageAdapter (modelName, store) {
this.modelName = modelName;
this.store = store;
StorageAdapter.instances[modelName] = this;
// Global listener - only listen once
if (!StorageAdapter.listener) {
StorageAdapter.listener = window.addEventListener ?
window.addEventListener('storage', StorageAdapter.onStorage, false) :
window.attachEvent('onstorage', StorageAdapter.onStorage);
}
}
// Hash of StorageAdapter instances
StorageAdapter.instances = {};
// Reference to the global onstorage handler
StorageAdapter.listener = null;
StorageAdapter.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 = StorageAdapter.instances[modelName];
if (!adapter)
return;
adapter.handleStorageEvent(key, id);
};
_.extend(StorageAdapter.prototype, {
handleStorageEvent: function (key, id) {
var attrs = this.getFromStorage(key);
if (!attrs)
this.trigger('destroy', id);
else
this.trigger('sync', id, attrs);
},
getFromStorage: function (key) {
try {
return JSON.parse(this.store.getItem(key));
} catch (err) {
return;
}
},
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);
this.store.setItem(this.getStorageKey(id), json);
},
remove: function (id) {
if (!id)
throw 'Cannot remove without id';
this.store.removeItem(this.getStorageKey(id));
}
}, Backbone.Events);
// Exports
_.extend(UniqueModel, {
ModelCache: ModelCache,
StorageAdapter: StorageAdapter
});
Backbone.UniqueModel = UniqueModel;
return UniqueModel;
}));