提交 79aaaa09 编写于 作者: C Catouse

* improve store.js.

上级 f9192c3c
......@@ -11,6 +11,7 @@
var lsName = 'localStorage';
var storage = window[lsName],
dataset,
old = window.store,
pageName = 'page_' + window.location.pathname + window.location.search;
......@@ -18,8 +19,42 @@
var Store = function() {
this.slience = true;
this.enable = (lsName in window) && window[lsName] && window[lsName].setItem;
if(!this.enable) {
dataset = {};
storage = {
getLength: function() {
var length = 0;
$.each(dataset, function() {
length++;
});
return length;
},
key: function(index) {
var key, i = 0;
$.each(dataset, function(k) {
if(i === index) {
key = k;
return false;
}
i++;
});
return key;
},
removeItem: function(key) {
delete dataset[key];
},
getItem: function(key) {
return dataset[key];
},
setItem: function(key, val) {
dataset[key] = val;
},
clear: function() {
dataset = {};
}
};
}
this.storage = storage;
this.page = this.get(pageName, {});
};
......@@ -141,7 +176,8 @@
/* Iterate all items with callback */
Store.prototype.forEach = function(callback) {
for(var i = storage.length - 1; i >= 0; i--) {
var length = this.length();
for(var i = length - 1; i >= 0; i--) {
var key = storage.key(i);
callback(key, this.get(key));
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册