-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement storage.js, list example API usage, and list TODO items. Th…
…is sheit still needs to get tested
- Loading branch information
0 parents
commit cb0198c
Showing
2 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
store.js | ||
======== | ||
|
||
store.js exposes a simple API for cross browser local store | ||
|
||
// Store 'marcus' at 'username' | ||
store.set('username', 'marcus') | ||
|
||
// Get 'username' | ||
store.get('username') | ||
|
||
// Delete 'username' | ||
store.delete('username') | ||
|
||
// Clear all keys | ||
store.clear() | ||
|
||
// Use JSON to stash an object (see http://www.json.org/json2.js) | ||
store.set('user', JSON.stringify({ name: 'marcus', likes: 'javascript' })) | ||
|
||
// Use JSON to retrieve an object (see http://www.json.org/json2.js) | ||
var user = JSON.parse(store.get('user')) | ||
alert(user.name + ' likes ' + user.likes) | ||
|
||
TODO | ||
---- | ||
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. | ||
|
||
- I believe underlying APIs can throw under certain conditions. Where do we need try/catch? | ||
- Write tests | ||
- Test in IE6 | ||
- Test in IE7 | ||
- Test in IE8 | ||
- Test in Firefox 2.0 | ||
- Test in Firefox 3.0 | ||
- Test in Firefox 3.5 | ||
- Test in Firefox 3.6 | ||
- Test in Safari 2 | ||
- Test in Safari 3 | ||
- Test in Safari 4 | ||
- Test in Safari 5 | ||
- Test in Chrome 4 | ||
- Test in Chrome 5 | ||
- Test in Opera 9 | ||
- Test in Opera 10 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
var store = (function(){ | ||
var api = {}, | ||
win = window, | ||
doc = win.document, | ||
name = 'localStorage', | ||
store | ||
|
||
api.set = function(key, value) {} | ||
api.get = function(key) {} | ||
api.delete = function(key) {} | ||
api.clear = function() {} | ||
|
||
if (win.globalStorage) { | ||
store = win.globalStorage[win.location.hostname] | ||
api.set = function(key, val) { store[key] = val } | ||
api.get = function(key) { return store[key].value } | ||
api.delete = function(key) { delete store[key] } | ||
api.clear = function() { for (var key in store ) { delete store[key] } } | ||
} else if (win.localStorage) { | ||
store = win.localStorage | ||
api.set = function(key, val) { store[key] = val } | ||
api.get = function(key) { return store[key] } | ||
api.delete = function(key) { delete store[key] } | ||
api.clear = function() { for (var key in store ) { delete store[key] } } | ||
} else if (Element.prototype.addBehavior) { | ||
store = doc.body.appendChild(doc.createElement('div')) | ||
store.style.display = 'none' | ||
// See http://msdn.microsoft.com/en-us/library/ms531081(v=VS.85).aspx | ||
// and http://msdn.microsoft.com/en-us/library/ms531424(v=VS.85).aspx | ||
store.addBehavior('#default#userData') | ||
store.load(name) | ||
api.set = function(key, val) { | ||
store.setAttribute(key, val) | ||
store.save(name) | ||
} | ||
api.get = function(key) { | ||
return store.getAttribute(key) | ||
} | ||
api.delete = function(key) { | ||
store.removeAttribute(key) | ||
store.save(name) | ||
} | ||
api.clear = function() { | ||
var attributes = store.XMLDocument.documentElement.attributes; | ||
for (var i=0, attr; attr = attributes[i]; i++) { | ||
store.removeAttribute(attr.name) | ||
} | ||
store.save(name) | ||
} | ||
} | ||
|
||
return api | ||
})(); |