Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #76 from deadlyicon/patch-1

I've seen better performance this pattern
  • Loading branch information...
commit 35b2f6bc1ab08fc2f37bb574646d9de0d3c4f295 2 parents 11577d8 + 4503edd
@brianleroux brianleroux authored
Showing with 12 additions and 17 deletions.
  1. +12 −17 src/Lawnchair.js
View
29 src/Lawnchair.js
@@ -4,33 +4,31 @@
* clientside json store
*
*/
-var Lawnchair = function () {
+var Lawnchair = function (options, callback) {
+ // ensure Lawnchair was called as a constructor
+ if (!(this instanceof Lawnchair)) return new Lawnchair(options, callback);
+
// lawnchair requires json
if (!JSON) throw 'JSON unavailable! Include http://www.json.org/json2.js to fix.'
// options are optional; callback is not
if (arguments.length <= 2 && arguments.length > 0) {
- var callback = (typeof arguments[0] === 'function') ? arguments[0] : arguments[1]
- , options = (typeof arguments[0] === 'function') ? {} : arguments[0]
+ callback = (typeof arguments[0] === 'function') ? arguments[0] : arguments[1];
+ options = (typeof arguments[0] === 'function') ? {} : arguments[0];
} else {
throw 'Incorrect # of ctor args!'
}
// TODO perhaps allow for pub/sub instead?
if (typeof callback !== 'function') throw 'No callback was provided';
- // ensure we init with this set to the Lawnchair prototype
- var self = (!(this instanceof Lawnchair))
- ? new Lawnchair(options, callback)
- : this
-
// default configuration
- self.record = options.record || 'record' // default for records
- self.name = options.name || 'records' // default name for underlying store
+ this.record = options.record || 'record' // default for records
+ this.name = options.name || 'records' // default name for underlying store
// mixin first valid adapter
var adapter
// if the adapter is passed in we try to load that only
if (options.adapter) {
- adapter = Lawnchair.adapters[self.indexOf(Lawnchair.adapters, options.adapter)]
+ adapter = Lawnchair.adapters[this.indexOf(Lawnchair.adapters, options.adapter)]
adapter = adapter.valid() ? adapter : undefined
// otherwise find the first valid adapter for this env
}
@@ -46,17 +44,14 @@ var Lawnchair = function () {
// yay! mixin the adapter
for (var j in adapter)
- self[j] = adapter[j]
+ this[j] = adapter[j]
// call init for each mixed in plugin
for (var i = 0, l = Lawnchair.plugins.length; i < l; i++)
- Lawnchair.plugins[i].call(self)
+ Lawnchair.plugins[i].call(this)
// init the adapter
- self.init(options, callback)
-
- // called as a function or as a ctor with new always return an instance
- return self
+ this.init(options, callback)
}
Lawnchair.adapters = []
Please sign in to comment.
Something went wrong with that request. Please try again.