Permalink
Browse files

Y U NO SOFTTAB

  • Loading branch information...
1 parent 712944b commit e877941c81da904517478bc15108696f40532429 @brianleroux brianleroux committed Jul 20, 2011
Showing with 40 additions and 39 deletions.
  1. +12 −12 src/Lawnchair.js
  2. +27 −26 src/adapters/dom.js
  3. +1 −1 src/adapters/window-name.js
View
@@ -74,7 +74,7 @@ Lawnchair.adapter = function (id, obj) {
// methods required to implement a lawnchair adapter
var implementing = 'adapter valid init keys save batch get exists all remove nuke'.split(' ')
, indexOf = this.prototype.indexOf
- // mix in the adapter
+ // mix in the adapter
for (var i in obj) {
if (indexOf(implementing, i) === -1) throw 'Invalid adapter! Nonstandard method: ' + i
}
@@ -113,27 +113,27 @@ Lawnchair.prototype = {
return -1
},
- // awesome shorthand callbacks as strings. this is shameless theft from dojo.
- lambda: function (callback) {
- return this.fn(this.record, callback)
+ // awesome shorthand callbacks as strings. this is shameless theft from dojo.
+ lambda: function (callback) {
+ return this.fn(this.record, callback)
},
// first stab at named parameters for terse callbacks; dojo: first != best // ;D
fn: function (name, callback) {
- return typeof callback == 'string' ? new Function(name, callback) : callback
+ return typeof callback == 'string' ? new Function(name, callback) : callback
},
- // returns a unique identifier (by way of Backbone.localStorage.js)
- // TODO investigate smaller UUIDs to cut on storage cost
- uuid: function () {
- var S4 = function () {
+ // returns a unique identifier (by way of Backbone.localStorage.js)
+ // TODO investigate smaller UUIDs to cut on storage cost
+ uuid: function () {
+ var S4 = function () {
return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
}
return (S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4());
- },
+ },
// a classic iterator
- each: function (callback) {
+ each: function (callback) {
var cb = this.lambda(callback)
// iterate from chain
if (this.__results) {
@@ -146,6 +146,6 @@ Lawnchair.prototype = {
})
}
return this
- }
+ }
// --
};
View
@@ -6,7 +6,8 @@
*/
//
// TODO does it make sense to be chainable all over the place?
-// chainable: nuke, remove, all, get, save, all
+// chainable: nuke, remove, all, get, save, all
+// perhaps time to look at promises?
// not chainable: valid, keys
//
Lawnchair.adapter('dom', {
@@ -15,7 +16,7 @@ Lawnchair.adapter('dom', {
return !!window.Storage
},
- init: function (options, callback) {
+ init: function (options, callback) {
// yay dom!
this.storage = window.localStorage
// indexer helper code
@@ -56,21 +57,21 @@ Lawnchair.adapter('dom', {
}
if (callback) this.fn(this.name, callback).call(this, this)
- },
-
+ },
+
save: function (obj, callback) {
- var key = obj.key || this.uuid()
+ var key = obj.key || this.uuid()
// if the key is not in the index push it on
if (!this.indexer.find(key)) this.indexer.add(key)
- // now we kil the key and use it in the store colleciton
+ // now we kil the key and use it in the store colleciton
delete obj.key;
- this.storage.setItem(key, JSON.stringify(obj))
- if (callback) {
- obj.key = key
+ this.storage.setItem(key, JSON.stringify(obj))
+ if (callback) {
+ obj.key = key
this.lambda(callback).call(this, obj)
- }
+ }
return this
- },
+ },
batch: function (ary, callback) {
var saved = []
@@ -112,7 +113,7 @@ Lawnchair.adapter('dom', {
},
// NOTE adapters cannot set this.__results but plugins do
// this probably should be reviewed
- all: function (callback) {
+ all: function (callback) {
var idx = this.indexer.all()
, r = []
, o
@@ -121,25 +122,25 @@ Lawnchair.adapter('dom', {
o.key = idx[i]
r.push(o)
}
- if (callback) this.fn(this.name, callback).call(this, r)
+ if (callback) this.fn(this.name, callback).call(this, r)
return this
- },
-
+ },
+
remove: function (keyOrObj, callback) {
var key = typeof keyOrObj === 'string' ? keyOrObj : keyOrObj.key
this.indexer.del(key)
- this.storage.removeItem(key)
- if (callback) this.lambda(callback).call(this)
+ this.storage.removeItem(key)
+ if (callback) this.lambda(callback).call(this)
return this
- },
-
+ },
+
nuke: function (callback) {
- this.all(function(r) {
- for (var i = 0, l = r.length; i < l; i++) {
- this.remove(r[i]);
- }
- if (callback) this.lambda(callback).call(this)
- })
+ this.all(function(r) {
+ for (var i = 0, l = r.length; i < l; i++) {
+ this.remove(r[i]);
+ }
+ if (callback) this.lambda(callback).call(this)
+ })
return this
- }
+ }
});
@@ -80,7 +80,7 @@ Lawnchair.adapter('window-name', (function(index, store) {
return this
},
- remove: function (keyOrArray, cb) {
+ remove: function (keyOrArray, cb) {
var del = this.isArray(keyOrArray) ? keyOrArray : [keyOrArray]
for (var i = 0, l = del.length; i < l; i++) {
delete store[del[i]]

0 comments on commit e877941

Please sign in to comment.