State in tostring (#529)

* add state to toString

* set initial state if possible

* add window assertion for tostring
This commit is contained in:
Yoshua Wuyts
2017-07-11 15:07:53 +02:00
committed by GitHub
parent 4e2ad6abc7
commit a703ac5c56
2 changed files with 6 additions and 3 deletions
+4 -2
View File
@@ -9,6 +9,7 @@ var nanohref = require('nanohref')
var nanoraf = require('nanoraf') var nanoraf = require('nanoraf')
var nanobus = require('nanobus') var nanobus = require('nanobus')
var assert = require('assert') var assert = require('assert')
var xtend = require('xtend')
module.exports = Choo module.exports = Choo
@@ -46,6 +47,7 @@ function Choo (opts) {
this.state = { events: this._events } this.state = { events: this._events }
// listen for title changes; available even when calling .toString() // listen for title changes; available even when calling .toString()
if (this._hasWindow) this.state.title = document.title
this.emitter.prependListener(this._events.DOMTITLECHANGE, function (title) { this.emitter.prependListener(this._events.DOMTITLECHANGE, function (title) {
assert.equal(typeof title, 'string', 'events.DOMTitleChange: title should be type string') assert.equal(typeof title, 'string', 'events.DOMTitleChange: title should be type string')
self.state.title = title self.state.title = title
@@ -143,7 +145,6 @@ Choo.prototype.start = function () {
})) }))
documentReady(function () { documentReady(function () {
self.state.title = document.title
self.emitter.emit(self._events.DOMCONTENTLOADED) self.emitter.emit(self._events.DOMCONTENTLOADED)
}) })
@@ -175,8 +176,9 @@ Choo.prototype.mount = function mount (selector) {
} }
Choo.prototype.toString = function (location, state) { Choo.prototype.toString = function (location, state) {
this.state = state || {} this.state = xtend({ events: xtend(this._events) }, this.state || {})
assert.notEqual(typeof window, 'object', 'choo.mount: window was found. .toString() must be called in Node, use .start() or .mount() if running in the browser')
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')
+2 -1
View File
@@ -32,7 +32,8 @@
"nanoraf": "^3.0.0", "nanoraf": "^3.0.0",
"nanorouter": "^2.0.0", "nanorouter": "^2.0.0",
"nanotiming": "^6.0.0", "nanotiming": "^6.0.0",
"scroll-to-anchor": "^1.0.0" "scroll-to-anchor": "^1.0.0",
"xtend": "^4.0.1"
}, },
"devDependencies": { "devDependencies": {
"browserify": "^14.3.0", "browserify": "^14.3.0",