forked from jeromegn/Backbone.localStorage
-
Notifications
You must be signed in to change notification settings - Fork 3
/
backbone.localStorage.js
116 lines (93 loc) · 3.72 KB
/
backbone.localStorage.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
(function(_, Backbone) {
// A simple module to replace `Backbone.sync` with *localStorage*-based
// persistence. Models are given GUIDS, and saved into a JSON object. Simple
// as that.
// 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());
};
Backbone.Storage = function (name,type) {
type = type || "local";
this.name = name;
this.type = type + "Storage";
var store = window[this.type].getItem(this.name);
this.records = (store && store.split(",")) || [];
}
_.extend(Backbone.Storage.prototype, {
// Save the current state of the **Store** to *localStorage*.
save: function() {
window[this.type].setItem(this.name, this.records.join(","));
},
// 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 = model.attributes.id = guid();
window[this.type].setItem(this.name + "-" + model.id, JSON.stringify(model));
this.records.push(model.id.toString());
this.save();
return model;
},
// Update a model by replacing its copy in `this.data`.
update: function(model) {
window[this.type].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;
},
// Retrieve a model from `this.data` by id.
find: function(model) {
return JSON.parse(window[this.type].getItem(this.name + "-" + model.id));
},
// Return the array of all models currently in storage.
findAll: function() {
return _.map(this.records, function(id) {
return JSON.parse(window[this.type].getItem(this.name + "-" + id))
}, this);
},
// Delete a model from `this.data`, returning it.
destroy: function(model) {
window[this.type].removeItem(this.name + "-" + model.id);
this.records = _.reject(this.records, function(record_id) {
return record_id == model.id.toString();
});
this.save();
return model;
}
});
var normalSync = Backbone.sync;
// Override `Backbone.sync` to use delegate to the model or collection's
// *localStorage* property, which should be an instance of `Store`.
// If there is no storage found, use the normal Backbone.sync
Backbone.sync = function(method, model, options, error) {
// Backwards compatibility with Backbone <= 0.3.3
if (typeof options == 'function') {
options = {
success: options,
error: error
};
}
var resp,
store = model.localStorage || model.sessionStorage;
if (!store && model.collection) {
store = model.collection.localStorage || model.collection.sessionStorage;
}
if (!store) {
return normalSync.apply(this, _.toArray(arguments));
}
switch (method) {
case "read": resp = model.id ? 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;
}
if (resp) {
options.success(resp);
} else {
options.error("Record not found");
}
};
})(_, Backbone);