Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce pre-render resolve event #603

Closed
wants to merge 2 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ function Choo (opts) {
PUSHSTATE: 'pushState',
NAVIGATE: 'navigate',
POPSTATE: 'popState',
RESOLVE: 'resolve',
RENDER: 'render'
}

Expand Down Expand Up @@ -99,10 +100,12 @@ Choo.prototype.start = function () {
var self = this
if (this._historyEnabled) {
this.emitter.prependListener(this._events.NAVIGATE, function () {
self.state.href = self._createLocation()
self.state.query = nanoquery(window.location.search)
if (self._loaded) {
self.emitter.emit(self._events.RENDER)
setTimeout(scrollToAnchor.bind(null, window.location.hash), 0)
var resList = self.emitter._listeners[self._events.RESOLVE]
var resCount = resList ? resList.length : 0;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we go with this implementation, it might be cleaner to move this logic to a countListeners or hasListener method in nanobus itself. But yeah, we'll cross that bridge if we get there.

self.emitter.emit(resCount > 0 ? self._events.RESOLVE : self._events.RENDER)
}
})

Expand Down Expand Up @@ -144,7 +147,6 @@ Choo.prototype.start = function () {
this.emitter.prependListener(self._events.RENDER, nanoraf(function () {
var renderTiming = nanotiming('choo.render')

self.state.href = self._createLocation()
var newTree = self.router(self.state.href)
assert.ok(newTree, 'choo.render: no valid DOM node returned for location ' + self.state.href)

Expand All @@ -157,6 +159,8 @@ Choo.prototype.start = function () {
morphTiming()

renderTiming()

setTimeout(scrollToAnchor.bind(null, window.location.hash), 0)
}))

documentReady(function () {
Expand Down