Skip to content

Commit

Permalink
Name the variable defining the store.js api "store" rather than "api".
Browse files Browse the repository at this point in the history
  • Loading branch information
marcuswestin committed Dec 13, 2011
1 parent c4a94c6 commit 48d6281
Showing 1 changed file with 33 additions and 33 deletions.
66 changes: 33 additions & 33 deletions store.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,30 @@
*/

;(function(){
var api = {},
var store = {},
win = window,
doc = win.document,
localStorageName = 'localStorage',
globalStorageName = 'globalStorage',
namespace = '__storejs__',
storage

api.disabled = false
api.set = function(key, value) {}
api.get = function(key) {}
api.remove = function(key) {}
api.clear = function() {}
api.transact = function(key, transactionFn) {
var val = api.get(key)
store.disabled = false
store.set = function(key, value) {}
store.get = function(key) {}
store.remove = function(key) {}
store.clear = function() {}
store.transact = function(key, transactionFn) {
var val = store.get(key)
if (typeof val == 'undefined') { val = {} }
transactionFn(val)
api.set(key, val)
store.set(key, val)
}

api.serialize = function(value) {
store.serialize = function(value) {
return JSON.stringify(value)
}
api.deserialize = function(value) {
store.deserialize = function(value) {
if (typeof value != 'string') { return undefined }
return JSON.parse(value)
}
Expand All @@ -63,17 +63,17 @@

if (isLocalStorageNameSupported()) {
storage = win[localStorageName]
api.set = function(key, val) { storage.setItem(key, api.serialize(val)) }
api.get = function(key) { return api.deserialize(storage.getItem(key)) }
api.remove = function(key) { storage.removeItem(key) }
api.clear = function() { storage.clear() }
store.set = function(key, val) { storage.setItem(key, store.serialize(val)) }
store.get = function(key) { return store.deserialize(storage.getItem(key)) }
store.remove = function(key) { storage.removeItem(key) }
store.clear = function() { storage.clear() }

} else if (isGlobalStorageNameSupported()) {
storage = win[globalStorageName][win.location.hostname]
api.set = function(key, val) { storage[key] = api.serialize(val) }
api.get = function(key) { return api.deserialize(storage[key] && storage[key].value) }
api.remove = function(key) { delete storage[key] }
api.clear = function() { for (var key in storage ) { delete storage[key] } }
store.set = function(key, val) { storage[key] = store.serialize(val) }
store.get = function(key) { return store.deserialize(storage[key] && storage[key].value) }
store.remove = function(key) { delete storage[key] }
store.clear = function() { for (var key in storage ) { delete storage[key] } }

} else if (doc.documentElement.addBehavior) {
var storage,
Expand Down Expand Up @@ -111,23 +111,23 @@
storageOwner.appendChild(storage)
storage.addBehavior('#default#userData')
storage.load(localStorageName)
var result = storeFunction.apply(api, args)
var result = storeFunction.apply(store, args)
storageOwner.removeChild(storage)
return result
}
}
api.set = withIEStorage(function(storage, key, val) {
storage.setAttribute(key, api.serialize(val))
store.set = withIEStorage(function(storage, key, val) {
storage.setAttribute(key, store.serialize(val))
storage.save(localStorageName)
})
api.get = withIEStorage(function(storage, key) {
return api.deserialize(storage.getAttribute(key))
store.get = withIEStorage(function(storage, key) {
return store.deserialize(storage.getAttribute(key))
})
api.remove = withIEStorage(function(storage, key) {
store.remove = withIEStorage(function(storage, key) {
storage.removeAttribute(key)
storage.save(localStorageName)
})
api.clear = withIEStorage(function(storage) {
store.clear = withIEStorage(function(storage) {
var attributes = storage.XMLDocument.documentElement.attributes
storage.load(localStorageName)
for (var i=0, attr; attr = attributes[i]; i++) {
Expand All @@ -138,14 +138,14 @@
}

try {
api.set(namespace, namespace)
if (api.get(namespace) != namespace) { api.disabled = true }
api.remove(namespace)
store.set(namespace, namespace)
if (store.get(namespace) != namespace) { store.disabled = true }
store.remove(namespace)
} catch(e) {
api.disabled = true
store.disabled = true
}

if (typeof module != 'undefined') { module.exports = api }
else if (typeof define === 'function' && define.amd) { define(api) }
else { this.store = api }
if (typeof module != 'undefined') { module.exports = store }
else if (typeof define === 'function' && define.amd) { define(store) }
else { this.store = store }
})()

0 comments on commit 48d6281

Please sign in to comment.