forked from educlever/ng-storage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathng-storage.js
43 lines (36 loc) · 1.23 KB
/
ng-storage.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
"use strict";
(function (angular) {
var module = angular.module("educ.ngStorage", []);
module.service("StorageService", [ "$window", function ($window) {
function InMemoryStorage() {
}
InMemoryStorage.prototype.setItem = function (key, value) {
this[key] = value;
};
InMemoryStorage.prototype.getItem = function (key) {
return typeof this[key] == "undefined" ? null : this[key];
};
InMemoryStorage.prototype.removeItem = function (key) {
delete this[key];
};
InMemoryStorage.prototype.clear = function () {
for (var key in this) {
if (this.hasOwnProperty(key)) {
this.removeItem(key);
}
}
};
InMemoryStorage.prototype.key = function (index) {
return Object.keys(this)[index];
};
try {
$window.localStorage._check_ = "it works !";
delete $window.localStorage._check_;
// console.log("using localStorage");
return $window.localStorage;
} catch (ex) {
// console.log("using InMemoryStorage");
return new InMemoryStorage();
}
}]);
})(angular);