Permalink
Browse files

fixed bad merge

  • Loading branch information...
2 parents 5b158a0 + 3adcc5c commit 97db457f10e59477aeb0b854107fce89ad2c052b @brianleroux brianleroux committed Jul 22, 2011
Showing with 127 additions and 70 deletions.
  1. +5 −0 doc/finding.md
  2. +1 −1 doc/initialization.md
  3. +1 −1 doc/iteration.md
  4. +2 −2 doc/removing.md
  5. +1 −1 doc/sugar.md
  6. +116 −64 src/adapters/dom.js
  7. +1 −1 test/lawnchair-spec.js
View
@@ -15,11 +15,16 @@ searching since you can just refer to a key.
That said, sometimes you may want to peruse the `Lawnchair` index.
+
:::JavaScript
Lawnchair(function() {
+ // get all the keys
this.keys(function(keys) {
keys.forEach(console.log)
})
+
+ // equiv to above
+ this.keys('keys.forEach(console.log)')
})
@@ -32,7 +32,7 @@ we want to setup our own parameter names for terse callbacks
people.save({me:'brian'}, 'console.log(person)')
// Or just use the orig reference created w/ the ctor
- ppl.destroy(me)
+ ppl.remove(me)
}, false)
View
@@ -10,7 +10,7 @@ Iterate the `Lawnchair` collection? Don't mind if I do!
// classic utility...
this.each(function(record, index) {
// ...and a plausable scenario
- console.log(record +' at '+ i +' o\'clock ')
+ console.log(record.type +' at '+ i +' o\'clock ')
// beer at 1 o'clock
// coffee at 2 o'clock
})
View
@@ -4,15 +4,15 @@ Removal
Deleting documents from the store is simple too.
:::JavaScript
- Lawnchair(function() {
+ Lawnchair({name:'records'}, function() {
// save a record
this.save({key:1}, function() {
this.all('console.log(records.length)')
// 1
- this.destroy(1, function() {
+ this.remove(1, function() {
this.all('console.log(records.length)')
// 0
})
View
@@ -10,7 +10,7 @@ my favorite feature of lawnchair shamelessly stolen from dojo. `lawnchair` dynam
:::JavaScript
- aquarium.sava([{key:'shark'}, {key:'whale'}], 'console.log(record)');
+ aquarium.save([{key:'shark'}, {key:'whale'}], 'console.log(record)');
You are correct in noticing the magical `record` parameter in the example above. by default, `lawnchair` will create either `record` or `records` for passed in parameters. you can change these by giving your lawnchair a `name` and `record` config in the constructor. you can read more about constructors below.
View
@@ -10,32 +10,24 @@
// perhaps time to look at promises?
// not chainable: valid, keys
//
-Lawnchair.adapter('dom', {
- // ensure we are in an env with localStorage
- valid: function () {
- return !!window.Storage
- },
-
- init: function (options, callback) {
- // yay dom!
- this.storage = window.localStorage
- // indexer helper code
- var self = this
- // the indexer is an encapsulation of the helpers needed to keep an ordered index of the keys
- this.indexer = {
+Lawnchair.adapter('dom', (function() {
+ var storage = window.localStorage
+ // the indexer is an encapsulation of the helpers needed to keep an ordered index of the keys
+ var indexer = function(name) {
+ return {
// the key
- key: self.name + '._index_',
+ key: name + '._index_',
// returns the index
all: function() {
- var a = JSON.parse(self.storage.getItem(this.key))
- if (a == null) self.storage.setItem(this.key, JSON.stringify([])) // lazy init
- return JSON.parse(self.storage.getItem(this.key))
+ var a = JSON.parse(storage.getItem(this.key))
+ if (a === null) storage.setItem(this.key, JSON.stringify([])) // lazy init
+ return JSON.parse(storage.getItem(this.key))
},
// adds a key to the index
add: function (key) {
var a = this.all()
a.push(key)
- self.storage.setItem(this.key, JSON.stringify(a))
+ storage.setItem(this.key, JSON.stringify(a))
},
// deletes a key from the index
del: function (key) {
@@ -44,7 +36,7 @@ Lawnchair.adapter('dom', {
for (var i = 0, l = a.length; i < l; i++) {
if (a[i] != key) r.push(a[i])
}
- self.storage.setItem(this.key, JSON.stringify(r))
+ storage.setItem(this.key, JSON.stringify(r))
},
// returns index for a key
find: function (key) {
@@ -55,53 +47,42 @@ Lawnchair.adapter('dom', {
return false
}
}
-
- if (callback) this.fn(this.name, callback).call(this, this)
- },
+ }
- save: function (obj, callback) {
- var key = obj.key || this.uuid()
- // if the key is not in the index push it on
- if (this.indexer.find(key) === false) this.indexer.add(key)
- // 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.lambda(callback).call(this, obj)
- }
- return this
- },
-
- batch: function (ary, callback) {
- var saved = []
- // not particularily efficient but this is more for sqlite situations
- for (var i = 0, l = ary.length; i < l; i++) {
- this.save(ary[i], function(r){
- saved.push(r)
- })
- }
- if (callback) this.lambda(callback).call(this, saved)
- return this
- },
-
- // accepts [options], callback
- keys: function() {
- // TODO support limit/offset options here
- var limit = options.limit || null
- , offset = options.offset || 0
- if (callback) this.lambda(callback).call(this, this.indexer.all())
- },
+ // adapter api
+ return {
- get: function (key, callback) {
- if (this.isArray(key)) {
- var r = []
- for (var i = 0, l = key.length; i < l; i++) {
- var obj = JSON.parse(this.storage.getItem(key[i]))
- if (obj) {
- obj.key = key[i]
- r.push(obj)
- }
+ // ensure we are in an env with localStorage
+ valid: function () {
+ return !!storage
+ },
+
+ init: function (options, callback) {
+ this.indexer = indexer(this.name)
+ if (callback) this.fn(this.name, callback).call(this, this)
+ },
+
+ save: function (obj, callback) {
+ var key = obj.key ? this.name + '.' + obj.key : this.name + '.' + this.uuid()
+ // if the key is not in the index push it on
+ if (this.indexer.find(key) === false) this.indexer.add(key)
+ // now we kil the key and use it in the store colleciton
+ delete obj.key;
+ storage.setItem(key, JSON.stringify(obj))
+ if (callback) {
+ obj.key = key.replace(this.name + '.', '')
+ this.lambda(callback).call(this, obj)
+ }
+ return this
+ },
+
+ batch: function (ary, callback) {
+ var saved = []
+ // not particularily efficient but this is more for sqlite situations
+ for (var i = 0, l = ary.length; i < l; i++) {
+ this.save(ary[i], function(r){
+ saved.push(r)
+ })
}
if (callback) this.lambda(callback).call(this, r)
} else {
@@ -111,6 +92,7 @@ Lawnchair.adapter('dom', {
}
return this
},
+/*dd>>>>>>> develop
// NOTE adapters cannot set this.__results but plugins do
// this probably should be reviewed
all: function (callback) {
@@ -144,3 +126,73 @@ Lawnchair.adapter('dom', {
return this
}
});
+*=======*/
+ if (callback) this.lambda(callback).call(this, saved)
+ return this
+ },
+
+ // accepts [options], callback
+ keys: function(callback) {
+ if (callback) {
+ var name = this.name
+ , keys = this.indexer.all().map(function(r){ return r.replace(name + '.', '') })
+ this.fn('keys', callback).call(this, keys)
+ }
+ return this // TODO options for limit/offset, return promise
+ },
+
+ get: function (key, callback) {
+ if (this.isArray(key)) {
+ var r = []
+ for (var i = 0, l = key.length; i < l; i++) {
+ var k = this.name + '.' + key[i]
+ , obj = JSON.parse(storage.getItem(k))
+ if (obj) {
+ obj.key = k
+ r.push(obj)
+ }
+ }
+ if (callback) this.lambda(callback).call(this, r)
+ } else {
+ var k = this.name + '.' + key
+ , obj = JSON.parse(storage.getItem(k))
+ if (obj) obj.key = k
+ if (callback) this.lambda(callback).call(this, obj)
+ }
+ return this
+ },
+ // NOTE adapters cannot set this.__results but plugins do
+ // this probably should be reviewed
+ all: function (callback) {
+ var idx = this.indexer.all()
+ , r = []
+ , o
+ , k
+ for (var i = 0, l = idx.length; i < l; i++) {
+ k = idx[i] //v
+ o = JSON.parse(storage.getItem(k))
+ o.key = k.replace(this.name + '.', '')
+ r.push(o)
+ }
+ if (callback) this.fn(this.name, callback).call(this, r)
+ return this
+ },
+
+ remove: function (keyOrObj, callback) {
+ var key = this.name + '.' + (typeof keyOrObj === 'string' ? keyOrObj : keyOrObj.key)
+ this.indexer.del(key)
+ 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)
+ })
+ return this
+ }
+}})());
@@ -386,4 +386,4 @@ test( 'remove functionality', function() {
});
//});
});
-});
+});

0 comments on commit 97db457

Please sign in to comment.