Skip to content

Commit cb0198c

Browse files
committed
Implement storage.js, list example API usage, and list TODO items. This sheit still needs to get tested
0 parents  commit cb0198c

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed

README

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
store.js
2+
========
3+
4+
store.js exposes a simple API for cross browser local store
5+
6+
// Store 'marcus' at 'username'
7+
store.set('username', 'marcus')
8+
9+
// Get 'username'
10+
store.get('username')
11+
12+
// Delete 'username'
13+
store.delete('username')
14+
15+
// Clear all keys
16+
store.clear()
17+
18+
// Use JSON to stash an object (see http://www.json.org/json2.js)
19+
store.set('user', JSON.stringify({ name: 'marcus', likes: 'javascript' }))
20+
21+
// Use JSON to retrieve an object (see http://www.json.org/json2.js)
22+
var user = JSON.parse(store.get('user'))
23+
alert(user.name + ' likes ' + user.likes)
24+
25+
TODO
26+
----
27+
I wrote store.js in the past hour looking at https://developer.mozilla.org/en/dom/store and http://msdn.microsoft.com/en-us/library/ms531424.aspx. I haven't tested it yet though.
28+
29+
- I believe underlying APIs can throw under certain conditions. Where do we need try/catch?
30+
- Write tests
31+
- Test in IE6
32+
- Test in IE7
33+
- Test in IE8
34+
- Test in Firefox 2.0
35+
- Test in Firefox 3.0
36+
- Test in Firefox 3.5
37+
- Test in Firefox 3.6
38+
- Test in Safari 2
39+
- Test in Safari 3
40+
- Test in Safari 4
41+
- Test in Safari 5
42+
- Test in Chrome 4
43+
- Test in Chrome 5
44+
- Test in Opera 9
45+
- Test in Opera 10

store.js

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
var store = (function(){
2+
var api = {},
3+
win = window,
4+
doc = win.document,
5+
name = 'localStorage',
6+
store
7+
8+
api.set = function(key, value) {}
9+
api.get = function(key) {}
10+
api.delete = function(key) {}
11+
api.clear = function() {}
12+
13+
if (win.globalStorage) {
14+
store = win.globalStorage[win.location.hostname]
15+
api.set = function(key, val) { store[key] = val }
16+
api.get = function(key) { return store[key].value }
17+
api.delete = function(key) { delete store[key] }
18+
api.clear = function() { for (var key in store ) { delete store[key] } }
19+
} else if (win.localStorage) {
20+
store = win.localStorage
21+
api.set = function(key, val) { store[key] = val }
22+
api.get = function(key) { return store[key] }
23+
api.delete = function(key) { delete store[key] }
24+
api.clear = function() { for (var key in store ) { delete store[key] } }
25+
} else if (Element.prototype.addBehavior) {
26+
store = doc.body.appendChild(doc.createElement('div'))
27+
store.style.display = 'none'
28+
// See http://msdn.microsoft.com/en-us/library/ms531081(v=VS.85).aspx
29+
// and http://msdn.microsoft.com/en-us/library/ms531424(v=VS.85).aspx
30+
store.addBehavior('#default#userData')
31+
store.load(name)
32+
api.set = function(key, val) {
33+
store.setAttribute(key, val)
34+
store.save(name)
35+
}
36+
api.get = function(key) {
37+
return store.getAttribute(key)
38+
}
39+
api.delete = function(key) {
40+
store.removeAttribute(key)
41+
store.save(name)
42+
}
43+
api.clear = function() {
44+
var attributes = store.XMLDocument.documentElement.attributes;
45+
for (var i=0, attr; attr = attributes[i]; i++) {
46+
store.removeAttribute(attr.name)
47+
}
48+
store.save(name)
49+
}
50+
}
51+
52+
return api
53+
})();

0 commit comments

Comments
 (0)