-
Notifications
You must be signed in to change notification settings - Fork 0
/
LocalStorageDataProvider.js
69 lines (64 loc) · 2.09 KB
/
LocalStorageDataProvider.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
(function (ns) {
"use strict";
/**
* @name wader.LocalStorageDataProvider
* @class Wader LocalStorage Data Provider
* @augments wader.ADataProvider
* @author sc0rp10 <[email protected]>
* @version 0.3
*/
ADataProvider.extend("wader.LocalStorageDataProvider",
/** @lends wader.LocalStorageDataProvider# */
{
_ls: window.localStorage,
/**
* @param {String} method
* @param {String} key
* @param {String} value
* @return {undefined}
*/
_makeRequest: function (method, key, value) {
var newKey = this.resource + "_" + key;
switch (method) {
case "get":
var result = this._ls.getItem(newKey);
try {
return JSON.parse(result);
} catch (e) {
return result;
}
case "post":
case "put":
return this._ls.setItem(newKey, JSON.stringify(value));
case "delete":
return this._ls.removeItem(newKey);
default:
break;
}
},
/**
* @param {String} pattern
* @return {Hash[]}
*/
getMulti: function (pattern) {
if (!pattern instanceof RegExp) {
throw new Error("Invalid params in LocalStorageDataProvider.getMulti: pattern must be instance of RegExp");
};
var items = [];
for (var key in this._ls) {
if (this._ls.hasOwnProperty(key)) {
if (pattern.test(key)) {
var obj = {};
key = key.replace(this.resource + "_", "")
obj[key] = this.get(key);
items.push(obj);
};
};
}
return items;
}
});
if (ns !== wader) {
ns.LocalStorageDataProvider = wader.LocalStorageDataProvider;
};
})(window.WADER_NS || window);