create stores lazily (#638)
This commit is contained in:
@@ -40,6 +40,7 @@ function Choo (opts) {
|
|||||||
this._hasWindow = typeof window !== 'undefined'
|
this._hasWindow = typeof window !== 'undefined'
|
||||||
this._createLocation = nanolocation
|
this._createLocation = nanolocation
|
||||||
this._loaded = false
|
this._loaded = false
|
||||||
|
this._stores = []
|
||||||
this._tree = null
|
this._tree = null
|
||||||
|
|
||||||
// properties that are part of the API
|
// properties that are part of the API
|
||||||
@@ -74,11 +75,14 @@ Choo.prototype.route = function (route, handler) {
|
|||||||
|
|
||||||
Choo.prototype.use = function (cb) {
|
Choo.prototype.use = function (cb) {
|
||||||
assert.equal(typeof cb, 'function', 'choo.use: cb should be type function')
|
assert.equal(typeof cb, 'function', 'choo.use: cb should be type function')
|
||||||
var msg = 'choo.use'
|
var self = this
|
||||||
msg = cb.storeName ? msg + '(' + cb.storeName + ')' : msg
|
this._stores.push(function () {
|
||||||
var endTiming = nanotiming(msg)
|
var msg = 'choo.use'
|
||||||
cb(this.state, this.emitter, this)
|
msg = cb.storeName ? msg + '(' + cb.storeName + ')' : msg
|
||||||
endTiming()
|
var endTiming = nanotiming(msg)
|
||||||
|
cb(self.state, self.emitter, self)
|
||||||
|
endTiming()
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
Choo.prototype.start = function () {
|
Choo.prototype.start = function () {
|
||||||
@@ -124,6 +128,10 @@ Choo.prototype.start = function () {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this._stores.forEach(function (initStore) {
|
||||||
|
initStore()
|
||||||
|
})
|
||||||
|
|
||||||
this._matchRoute()
|
this._matchRoute()
|
||||||
this._tree = this._prerender(this.state)
|
this._tree = this._prerender(this.state)
|
||||||
assert.ok(this._tree, 'choo.start: no valid DOM node returned for location ' + this.state.href)
|
assert.ok(this._tree, 'choo.start: no valid DOM node returned for location ' + this.state.href)
|
||||||
@@ -192,6 +200,11 @@ Choo.prototype.toString = function (location, state) {
|
|||||||
assert.equal(typeof location, 'string', 'choo.toString: location should be type string')
|
assert.equal(typeof location, 'string', 'choo.toString: location should be type string')
|
||||||
assert.equal(typeof this.state, 'object', 'choo.toString: state should be type object')
|
assert.equal(typeof this.state, 'object', 'choo.toString: state should be type object')
|
||||||
|
|
||||||
|
// TODO: pass custom state down to each store.
|
||||||
|
this._stores.forEach(function (initStore) {
|
||||||
|
initStore()
|
||||||
|
})
|
||||||
|
|
||||||
this._matchRoute(location)
|
this._matchRoute(location)
|
||||||
var html = this._prerender(this.state)
|
var html = this._prerender(this.state)
|
||||||
assert.ok(html, 'choo.toString: no valid value returned for the route ' + location)
|
assert.ok(html, 'choo.toString: no valid value returned for the route ' + location)
|
||||||
|
|||||||
Reference in New Issue
Block a user