forked from RubyLouvre/mass-Framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
store.js
208 lines (201 loc) · 8.41 KB
/
store.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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
define("store", this.JSON && JSON.parse ? ["support"] :["$support","./json2"], function($){
var store = {
//一些接口(空实现)
disabled: false,
set : function(key, value) {},
get: function(key) {},
remove : function(key) {},
clear : function() {},
transact : function(key, defaultVal, transactionFn) {
var val = store.get(key)
if (transactionFn == null) {
transactionFn = defaultVal
defaultVal = null
}
if (typeof val == 'undefined') {
val = defaultVal || {}
}
transactionFn(val)
store.set(key, val)
},
getAll : function() {},
serialize : function(value) {
return JSON.stringify(value)
},
deserialize : function(value) {
if (typeof value != 'string') {
return undefined
}
return JSON.parse(value)
}
}
//http://wojodesign.com/full-browser-support-for-localstorage-without-cookies/
//http://mathiasbynens.be/notes/localstorage-pattern
var name = "test"+ (new Date-0), win = window, localStorageName = "localStorage",storage
var supportLocalStorage = $.support.localStorage = false;
try {
localStorage.setItem(name,"mass");
localStorage.removeItem(name);
supportLocalStorage = true;
}catch(e){}
var supportGlobalStorage = $.support.globalStorage = false;
try {
supportGlobalStorage = win["globalStorage"][win.location.hostname]
}catch(e){}
if (supportLocalStorage) {
storage = localStorage;
$.mix(store,{//重写
set: function(key, val) {
if (val === undefined) {
return store.remove(key)
}
storage.setItem(key, store.serialize(val))
},
get: function(key) {
return store.deserialize(storage.getItem(key))
},
remove: function(key) {
storage.removeItem(key)
},
clear: function() {
storage.clear()
},
getAll: function() {
var ret = {}
for (var i=0; i<storage.length; ++i) {
var key = storage.key(i)
ret[key] = store.get(key)
}
return ret
}
})
}else if(supportGlobalStorage){
storage = supportGlobalStorage
$.mix(store,{//重写
set: function(key, val) {
if (val === undefined) {
return store.remove(key)
}
storage[key] = store.serialize(val)
},
get: function(key) {
return store.deserialize(storage[key] && storage[key].value)
},
remove: function(key) {
delete storage[key]
},
clear: function() {
for (var key in storage ) {
delete storage[key]
}
},
getAll: function() {
var ret = {}
for (var i=0; i<storage.length; ++i) {
var key = storage.key(i)
ret[key] = store.get(key)
}
return ret
}
})
}else if ( $.html.addBehavior) {
var storageOwner,
storageContainer
//由于#userData的存储仅适用于特定的路径,
//我们需要以某种方式关联我们的数据到一个特定的路径。我们选择/favicon.ico作为一个非常安全的目标,
//因为所有的浏览器都发出这个URL请求,而且这个请求即使是404也不会有危险。
//我们可以通过一个ActiveXObject(htmlfle)对象的文档来干这事。
//(参见:http://msdn.microsoft.com/en-us/library/aa752574(v = VS.85). aspx)
//因为iframe的访问规则允许直接访问和操纵文档中的元素,即使是404。
//这文档可以用来代替当前文档(这被限制在当前路径)执行#userData的存储。
try {
storageContainer = new ActiveXObject('htmlfile')
storageContainer.open()
storageContainer.write('<s' + 'cript>document.w=window</s' + 'cript><iframe src="/favicon.ico"></frame>')
storageContainer.close()
storageOwner = storageContainer.w.frames[0].document
storage = storageOwner.createElement('div')
} catch(e) {
storage = document.createElement('div')
storageOwner = document.body
}
function withIEStorage(storeFunction) {
return function() {
var args = Array.apply([],arguments);
args.unshift(storage)
// http://msdn.microsoft.com/en-us/library/ms531081(v=VS.85).aspx
// http://msdn.microsoft.com/en-us/library/ms531424(v=VS.85).aspx
storageOwner.appendChild(storage)
storage.addBehavior('#default#userData')
storage.load(localStorageName)
var result = storeFunction.apply(store, args)
storageOwner.removeChild(storage)
return result
}
}
// In IE7, keys may not contain special chars. See all of https://github.com/marcuswestin/store.js/issues/40
var forbiddenCharsRegex = new RegExp("[!\"#$%&'()*+,/\\\\:;<=>?@[\\]^`{|}~]", "g")
function ieKeyFix(key) {
// 不能以数字开头
// See https://github.com/marcuswestin/store.js/issues/40#issuecomment-4617842
return key.replace(forbiddenCharsRegex, '___')
}
$.mix(store,{//重写
set: withIEStorage(function(storage, key, val) {
key = ieKeyFix(key)
if (val === undefined) {
return store.remove(key)
}
storage.setAttribute(key, store.serialize(val))
storage.save(localStorageName)
}),
get: withIEStorage(function(storage, key) {
key = ieKeyFix(key)
return store.deserialize(storage.getAttribute(key))
}),
remove: withIEStorage(function(storage, key) {
key = ieKeyFix(key)
storage.removeAttribute(key)
storage.save(localStorageName)
}),
clear: withIEStorage(function(storage) {
var attributes = storage.XMLDocument.documentElement.attributes
storage.load(localStorageName)
for (var i=0, attr; attr=attributes[i]; i++) {
storage.removeAttribute(attr.name)
}
storage.save(localStorageName)
}),
getAll: withIEStorage(function(storage) {
var attributes = storage.XMLDocument.documentElement.attributes
storage.load(localStorageName)
var ret = {}
for (var i=0, attr; attr=attributes[i]; ++i) {
ret[attr] = store.get(attr)
}
return ret
})
})
}
try {
store.set(localStorageName, localStorageName)
if (store.get(localStorageName) != localStorageName) {
store.disabled = true
}
store.remove(localStorageName);
} catch(e) {
store.disabled = true
}
return store;
})
/*这里提供了一个用cookie实现本地储存的方案 https://developer.mozilla.org/en/DOM/Storage
其他有用的资料
http://www.cnblogs.com/NNUF/archive/2012/06/01/2531436.html
http://www.cnblogs.com/zjcn/archive/2012/07/03/2575026.html
http://dev-test.nemikor.com/web-storage/support-test/
http://arty.name/localstorage.html
firefox中对file://协议的不支持.
当你在firefox中打开一个本地的html文件的时候,也就是使用file://协议运行一个页面的时候,localStorage是不起作用的.无法设置和获取localStorage.
其实,本地调试这种方式已经很落后了,至少应该再127.0.0.1的环境中调试吧,这样调试的时候localStorage是工作的,有的人说这是一个firefox的bug.
但是看到一个解释,我觉得还是挺靠谱的,在file协议中,本来就没有domain的概念,而localStorage是根据domain来生效的.所以从道理上来讲就不应该在file://协议上生效.
*/