Skip to content

Commit

Permalink
feat: call unnamed root node listener on first route transition (toSt…
Browse files Browse the repository at this point in the history
…ate is null)
  • Loading branch information
troch committed Jul 23, 2015
1 parent 241c80f commit 618ef81
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
17 changes: 9 additions & 8 deletions modules/Router5.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,19 +108,19 @@ class Router5 {
let startPath = this.getLocation();
let startState = this.matchPath(startPath)

let cb = (err) => {
let cb = (err, state) => {
window.addEventListener('popstate', this.onPopState.bind(this))
if (done) done(err)
if (done) done(err, state)
}

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

if (startState) {
this.lastStateAttempt = startState
this._transition(this.lastStateAttempt, this.lastKnownState, (err) => {
this._transition(this.lastStateAttempt, this.lastKnownState, (err, state) => {
if (!err) {
window.history.replaceState(this.lastKnownState, '', this.buildUrl(startState.name, startState.params))
cb(null)
cb(null, state)
}
else if (opts.defaultRoute) navigateToDefault()
else cb(err)
Expand Down Expand Up @@ -384,7 +384,7 @@ class Router5 {
this._invokeListeners('=' + toState.name, toState, fromState)
this._invokeListeners('*', toState, fromState)

if (done) done(null)
if (done) done(null, toState)
})

this._tr = tr
Expand All @@ -411,7 +411,8 @@ class Router5 {

if (!path) throw new Error(`Could not find route "${name}"`)

this.lastStateAttempt = makeState(name, params, path)
let toState = makeState(name, params, path)
this.lastStateAttempt = toState
let sameStates = this.lastKnownState ? this.areStatesEqual(this.lastKnownState, this.lastStateAttempt) : false

// Do not proceed further if states are the same and no reload
Expand All @@ -422,14 +423,14 @@ class Router5 {
}

// Transition and amend history
return this._transition(this.lastStateAttempt, this.lastKnownState, (err) => {
return this._transition(toState, this.lastKnownState, (err, state) => {
if (err) {
if (done) done(err)
return
}

window.history[opts.replace ? 'replaceState' : 'pushState'](this.lastStateAttempt, '', url)
if (done) done(null)
if (done) done(null, state)
})
}
}
Expand Down
4 changes: 2 additions & 2 deletions modules/transition.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export default function transition(router, toState, fromState, callback) {

let toDeactivate = fromStateIds.slice(i).reverse()
let toActivate = toStateIds.slice(i)
let intersection = fromState ? (i > 0 ? fromStateIds[i - 1] : '') : null
let intersection = fromState && i > 0 ? fromStateIds[i - 1] : ''

let canDeactivate = (toState, fromState, cb) => {
if (cancelled) done()
Expand Down Expand Up @@ -66,7 +66,7 @@ export default function transition(router, toState, fromState, callback) {
}
}

let pipeline = fromState ? [canDeactivate, canActivate, nodeListener] : [canActivate]
let pipeline = fromState ? [canDeactivate, canActivate, nodeListener] : [canActivate, nodeListener]
asyncProcess(pipeline, toState, fromState, done)

return cancel
Expand Down
14 changes: 14 additions & 0 deletions tests/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,20 @@ function testRouter(useHash) {
}).toThrow();
});

it('should call root node listener on first transition', function (done) {
router.stop();
router.setOption('defaultRoute', 'home');
window.history.replaceState({}, '', base);
spyOn(listeners, 'global');
router.addNodeListener('', listeners.global);

router.start(function (err, state) {
expect(state).toEqual({name: 'home', path: '/home', params: {}});
expect(listeners.global).toHaveBeenCalled();
done();
});
});

it('should be able to navigate to routes', function (done) {
router.navigate('users.view', {id: 123}, {}, function (err) {
expect(getPath(useHash)).toBe(getExpectedPath(useHash, '/users/view/123'));
Expand Down

0 comments on commit 618ef81

Please sign in to comment.