forked from jeromegn/Backbone.localStorage
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbackbone.domStorage.js
418 lines (353 loc) · 12.2 KB
/
backbone.domStorage.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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/**
* Backbone localStorage and sessionStorage Adapter
* https://github.com/mikeedwards/Backbone.DOMStorage
*/
//A localStorage/sessionStorage polyfill
//see https://gist.github.com/350433
(function () {
var storageAvailable = true;
try {
if (typeof window.localStorage !== 'undefined')
{
//testing to see if localStorage is available, but restricted
//by private browsing (e.g. on an iPad)
window.localStorage.setItem('testLocalStorage','success');
window.localStorage.removeItem('testLocalStorage');
}
} catch (err) {
storageAvailable = false;
}
if (!storageAvailable
|| typeof window.localStorage === 'undefined'
|| typeof window.sessionStorage === 'undefined') (function () {
var Storage = function (type) {
function createCookie(name, value, days) {
var date, expires;
if (days) {
date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
expires = "; expires="+date.toGMTString();
} else {
expires = "";
}
document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name) {
var nameEQ = name + "=",
ca = document.cookie.split(';'),
i, c;
for (i=0; i < ca.length; i++) {
c = ca[i];
while (c.charAt(0)==' ') {
c = c.substring(1,c.length);
}
if (c.indexOf(nameEQ) == 0) {
return c.substring(nameEQ.length,c.length);
}
}
return null;
}
function setData(data) {
data = JSON.stringify(data);
if (type === 'session') {
window.name = data;
} else {
createCookie('localStorage', data, 365);
}
}
function clearData() {
if (type === 'session') {
window.name = '';
} else {
createCookie('localStorage', '', 365);
}
}
function getData() {
var data = type === 'session' ? window.name : readCookie('localStorage');
return data ? JSON.parse(data) : {};
}
// initialise if there's already data
var data = getData();
return {
length: 0,
clear: function () {
data = {};
this.length = 0;
clearData();
},
getItem: function (key) {
return data[key] === undefined ? null : data[key];
},
key: function (i) {
// not perfect, but works
var ctr = 0;
for (var k in data) {
if (ctr === i) return k;
else ctr++;
}
return null;
},
removeItem: function (key) {
delete data[key];
this.length--;
setData(data);
},
setItem: function (key, value) {
data[key] = value+''; // forces the value to a string
this.length++;
setData(data);
}
};
};
if (storageAvailable) {
if (typeof window.localStorage === 'undefined') window.localStorage = new Storage('local');
if (typeof window.sessionStorage === 'undefined') window.sessionStorage = new Storage('session');
} else {
window.privateLocalStorage = new Storage('local');
window.privateSessionStorage = new Storage('session');
}
})();
})();
(function() {
// A simple module to replace `Backbone.sync` with *localStorage*-based
// persistence. Models are given GUIDS, and saved into a JSON object. Simple
// as that.
// Hold reference to Underscore.js and Backbone.js in the closure in order
// to make things work even if they are removed from the global namespace
var _ = this._;
var Backbone = this.Backbone;
// Generate four random hex digits.
function S4() {
return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
};
// Generate a pseudo-GUID by concatenating random hexadecimal.
function guid() {
return (S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4());
};
// Our Store is represented by a single JS object in *localStorage*. Create it
// with a meaningful name, like the name you'd give a table.
// window.Store is deprectated, use Backbone.LocalStorage instead
Backbone.LocalStorage = window.Store = function(name) {
this.name = name;
var store = this.localStorage().getItem(this.name) || "";
this.records = (store && store.split(",")) || [];
};
_.extend(Backbone.LocalStorage.prototype, {
// Save the current state of the **Store** to *localStorage*.
save: function() {
this.localStorage().setItem(this.name, this.records.join(","));
},
// Fetches an item from local storage and returns an empty object if it doesn't exist
// cf. https://github.com/leaguevine/leaguevine-ultistats/commit/056e01512083fb27d5a4d67c4c733e0f57fc59ff
safeGet: function(name) {
var obj = this.localStorage().getItem(name);
if (!obj) {
return '{}';
}
return obj;
},
// Add a model, giving it a (hopefully)-unique GUID, if it doesn't already
// have an id of it's own.
create: function(model) {
if (!model.id) {
model.id = guid();
model.set(model.idAttribute, model.id);
}
this.localStorage().setItem(this.name+"-"+model.id, JSON.stringify(model));
this.records.push(model.id.toString());
this.save();
return model.toJSON();
},
// Update a model by replacing its copy in `this.data`.
update: function(model) {
this.localStorage().setItem(this.name+"-"+model.id, JSON.stringify(model));
if (!_.include(this.records, model.id.toString())) this.records.push(model.id.toString()); this.save();
return model.toJSON();
},
// Retrieve a model from `this.data` by id.
find: function(model) {
return JSON.parse(this.safeGet(this.name+"-"+model.id));
},
// Return the array of all models currently in storage.
findAll: function() {
return _(this.records).chain()
.map(function(id){
var obj = JSON.parse(this.safeGet(this.name+"-"+id));
return _.isEmpty(obj) ? false : obj;
}, this)
.compact()
.value();
},
// Delete a model from `this.data`, returning it.
destroy: function(model) {
this.localStorage().removeItem(this.name+"-"+model.id);
this.records = _.reject(this.records, function(record_id){return record_id == model.id.toString();});
this.save();
return model;
},
localStorage: function() {
var storage;
if (window.privateLocalStorage) {
storage = window.privateLocalStorage;
} else {
storage = localStorage;
}
return storage;
}
});
// localSync delegate to the model or collection's
// *localStorage* property, which should be an instance of `Store`.
// window.Store.sync and Backbone.localSync is deprectated, use Backbone.LocalStorage.sync instead
Backbone.LocalStorage.sync = window.Store.sync = Backbone.localSync = function(method, model, options, error) {
var store = model.localStorage || model.collection.localStorage,
resp,
error = "Record not found",
syncDfd = $.Deferred && $.Deferred(); //If $ is having Deferred - use it.
// Backwards compatibility with Backbone <= 0.3.3
if (typeof options == 'function') {
options = {
success: options,
error: error
};
}
try {
switch (method) {
case "read": resp = model.id != undefined ? store.find(model) : store.findAll(); break;
case "create": resp = store.create(model); break;
case "update": resp = store.update(model); break;
case "delete": resp = store.destroy(model); break;
}
} catch (e) { error = e; }
if (resp) {
options.success(resp);
if (syncDfd) {
syncDfd.resolve();
}
} else {
options.error("Record not found");
if (syncDfd) {
syncDfd.reject();
}
}
return syncDfd && syncDfd.promise();
};
// Our Store is represented by a single JS object in *sessionStorage*. Create it
// with a meaningful name, like the name you'd give a table.
// window.Store is deprectated, use Backbone.SessionStorage instead
Backbone.SessionStorage = window.SessionStore = function(name) {
this.name = name;
var store = this.sessionStorage().getItem(this.name) || "";
this.records = (store && store.split(",")) || [];
};
_.extend(Backbone.SessionStorage.prototype, {
// Save the current state of the **Store** to *sessionStorage*.
save: function() {
this.sessionStorage().setItem(this.name, this.records.join(","));
},
// Fetches an item from local storage and returns an empty object if it doesn't exist
// cf. https://github.com/leaguevine/leaguevine-ultistats/commit/056e01512083fb27d5a4d67c4c733e0f57fc59ff
safeGet: function(name) {
var obj = this.sessionStorage().getItem(name);
if (!obj) {
return '{}';
}
return obj;
},
// Add a model, giving it a (hopefully)-unique GUID, if it doesn't already
// have an id of it's own.
create: function(model) {
if (!model.id) {
model.id = guid();
model.set(model.idAttribute, model.id);
}
this.sessionStorage().setItem(this.name+"-"+model.id, JSON.stringify(model));
this.records.push(model.id.toString());
this.save();
return model.toJSON();
},
// Update a model by replacing its copy in `this.data`.
update: function(model) {
this.sessionStorage().setItem(this.name+"-"+model.id, JSON.stringify(model));
if (!_.include(this.records, model.id.toString())) this.records.push(model.id.toString()); this.save();
return model.toJSON();
},
// Retrieve a model from `this.data` by id.
find: function(model) {
return JSON.parse(this.safeGet(this.name+"-"+model.id));
},
// Return the array of all models currently in storage.
findAll: function() {
return _(this.records).chain()
.map(function(id){
var obj = JSON.parse(this.safeGet(this.name+"-"+id));
return _.isEmpty(obj) ? false : obj;
}, this)
.compact()
.value();
},
// Delete a model from `this.data`, returning it.
destroy: function(model) {
this.sessionStorage().removeItem(this.name+"-"+model.id);
this.records = _.reject(this.records, function(record_id){return record_id == model.id.toString();});
this.save();
return model;
},
sessionStorage: function() {
var storage;
if (window.privateLocalStorage) {
storage = window.privateSessionStorage;
} else {
storage = sessionStorage;
}
return storage;
}
});
// sessionSync delegate to the model or collection's
// *sessionStorage* property, which should be an instance of `SessionStore`.
Backbone.SessionStorage.sync = Backbone.sessionSync = function(method, model, options, error) {
var store = model.sessionStorage || model.collection.sessionStorage,
resp,
error = "Record not found",
syncDfd = $.Deferred && $.Deferred(); //If $ is having Deferred - use it.
// Backwards compatibility with Backbone <= 0.3.3
if (typeof options == 'function') {
options = {
success: options,
error: error
};
}
try {
switch (method) {
case "read": resp = model.id != undefined ? store.find(model) : store.findAll(); break;
case "create": resp = store.create(model); break;
case "update": resp = store.update(model); break;
case "delete": resp = store.destroy(model); break;
}
} catch (e) { error = e; }
if (resp) {
options.success(resp);
if (syncDfd) syncDfd.resolve();
} else {
options.error(error);
if (syncDfd) syncDfd.reject();
}
return syncDfd && syncDfd.promise();
};
Backbone.ajaxSync = Backbone.sync;
Backbone.getSyncMethod = function(model) {
if(model.localStorage || (model.collection && model.collection.localStorage))
{
return Backbone.LocalStorage.sync;
}
if(model.sessionStorage || (model.collection && model.collection.sessionStorage))
{
return Backbone.SessionStorage.sync;
}
return Backbone.ajaxSync;
};
// Override 'Backbone.sync' to default to localSync,
// the original 'Backbone.sync' is still available in 'Backbone.ajaxSync'
Backbone.sync = function(method, model, options, error) {
return Backbone.getSyncMethod(model).apply(this, [method, model, options, error]);
};
})();