Skip to content

Commit

Permalink
fix: call transition on start when matched route present (call canAct…
Browse files Browse the repository at this point in the history
…ivate methods)
  • Loading branch information
troch committed Jul 22, 2015
1 parent 9251bf9 commit 0efc4e6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 14 deletions.
27 changes: 19 additions & 8 deletions modules/Router5.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,21 +90,32 @@ export default class Router5 {
*/
start(done) {
if (this.started) return this
this.started = true
this.started = true
let opts = this.options

// Try to match starting path name
let startPath = this.getLocation();
let startState = this.matchPath(startPath)

if (startState) {
this.lastKnownState = startState
window.history.replaceState(this.lastKnownState, '', this.buildUrl(startState.name, startState.params))
if (done) done()
} else if (this.options.defaultRoute) {
this.navigate(this.options.defaultRoute, this.options.defaultParams, {replace: true}, done)
let cb = (err) => {
window.addEventListener('popstate', this.onPopState.bind(this))
done(err)
}

let navigateToDefault = () => this.navigate(opts.defaultRoute, opts.defaultParams, {replace: true}, cb)

if (startState) {
this.lastStateAttempt = startState
this._transition(this.lastStateAttempt, this.lastKnownState, (err) => {
if (!err) {
window.history.replaceState(this.lastKnownState, '', this.buildUrl(startState.name, startState.params))
cb(null)
}
else if (opts.defaultRoute) navigateToDefault()
else cb(err)
})
} else if (opts.defaultRoute) navigateToDefault()
// Listen to popstate
window.addEventListener('popstate', this.onPopState.bind(this))
return this
}

Expand Down
12 changes: 6 additions & 6 deletions modules/constants.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const constants = {
ROUTER_NOT_STARTED : 0,
SAME_STATES : 1,
CANNOT_DEACTIVATE : 2,
CANNOT_ACTIVATE : 3,
NODE_LISTENER_ERR : 4,
TRANSITION_CANCELLED : 5
ROUTER_NOT_STARTED : 1,
SAME_STATES : 2,
CANNOT_DEACTIVATE : 3,
CANNOT_ACTIVATE : 4,
NODE_LISTENER_ERR : 5,
TRANSITION_CANCELLED : 6
}

export default constants

0 comments on commit 0efc4e6

Please sign in to comment.