forked from robmoorman/backbone-localstorage
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackbone-localstorage.js
More file actions
161 lines (134 loc) · 4.85 KB
/
backbone-localstorage.js
File metadata and controls
161 lines (134 loc) · 4.85 KB
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
(function(root, Backbone){
var prefix = 'unknown';
var version = '';
// Save the previous value of the `Backbone.sync` method, so that it can be called later on.
var previousSync = root.Backbone.sync;
// Save a reference to the global Backbone object
var LocalStorage = root.Backbone.LocalStorage = {};
// Current version of the library. Keep in sync with `package.json` and `bower.json`.
LocalStorage.VERSION = '0.3.1';
/**
* @param {string}
* @param {object}
*/
LocalStorage._setData = function(id, value){
root.localStorage.setItem(prefix+':'+id, JSON.stringify(value));
};
/**
* @param {string}
* @return {object}
*/
LocalStorage._getData = function(id){
var data = root.localStorage.getItem(prefix+':'+id);
return typeof data === 'string' ? JSON.parse(data) : data;
};
/**
* @param {boolean}
*/
LocalStorage._clear = function(ignorePrefixCondition){
if (!ignorePrefixCondition){
for(var prop in root.localStorage) {
if(prop.indexOf(prefix) === 0) {
root.localStorage.removeItem(prop);
}
}
}
else {
root.localStorage.clear();
}
};
/**
* @param {string}
* @param {Backbone.Model}
* @param {object}
*/
LocalStorage.sync = function(method, model, options){
if (method === 'read' && ((typeof this.localStorage === 'object') || (this.localStorage !== undefined && this.localStorage !== null && this.localStorage.toString().toLowerCase() === 'true'))){
// Retrieve unique id under which the data will be stored, if no id found use the id of the model
var id = _.result(model, 'url');
// Retrieve timestamp from localStorage
var timestamp = LocalStorage._getData(id+':timestamp');
if (id !== undefined && id !== null){
var data = LocalStorage._getData(id);
if (data === null || data === undefined || options.forceRefresh || (timestamp !== undefined && model.localStorage.maxRefresh && (((new Date().getTime()) - timestamp) > model.localStorage.maxRefresh))){
var success = options.success;
options.success = function(response, status, xhr){
try{
LocalStorage._setData(id, response);
LocalStorage._setData(id+':timestamp', new Date().getTime());
}
catch(err){
if(err === QUOTA_EXCEEDED_ERR){
LocalStorage._clear();
}
}
if (success){
success.apply(this, arguments);
}
};
previousSync.apply(this, [method, model, options]);
}
else {
options.success.apply( this, [data, 'success', null]);
}
}
else {
previousSync.apply(this, arguments);
}
}
else {
previousSync.apply(this, arguments);
}
};
/**
* @param {string}
*/
LocalStorage.setVersion = function(value){
var versionInStorage = LocalStorage._getData('version');
// clear localStorage (only prefixed data) if version differs
if (versionInStorage !== null && versionInStorage !== value){
LocalStorage._clear(false);
}
// store new version in storage
LocalStorage._setData('version', value);
version = value;
};
/**
* @return {string}
*/
LocalStorage.getVersion = function(){
return version;
};
/**
* @param {string}
*/
LocalStorage.setPrefix = function(value){
prefix = value;
};
/**
* @return {string}
*/
LocalStorage.getPrefix = function(){
return prefix;
};
/**
* @return {boolean}
*/
LocalStorage.isSupported = function(){
try {
var supported = root.localStorage !== undefined;
if (supported){
localStorage.setItem('storage', '');
localStorage.removeItem('storage');
}
return supported;
}
catch(err){
return false;
}
};
// Override Backbone.sync method when localStorage is supported.
if (LocalStorage.isSupported()){
root.Backbone.sync = LocalStorage.sync;
}
}).call(this, window, Backbone);