Skip to content

Commit

Permalink
fix: bug with reload option and same states
Browse files Browse the repository at this point in the history
  • Loading branch information
troch committed Jul 2, 2015
1 parent 78c1a51 commit 2317287
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 25 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,21 @@ on server capabilities!

It is aimed at applications rendering a tree of components, but can easily be used elsewhere.
This router is library and framework agnostic, and makes no asumption on your implementation.
It favours __covention over configuration__, by giving you the means to observes route changes
and react to them. Afterall, why treat route changes any different than data changes?
It favours __covention over configuration__, by giving you the means to observe route changes
and to react to them. Afterall, why treat route changes any different than data changes?

## Features

- __Use of hash (#)__
- __Default start route__: a default route to navigate to on load if the current URL doesn't match any route. Similar to `$routeProvider.otherwise()` in _Angular ngRoute_ module.
- __Start__ and __stop__ router
- __Nested named routes__: routes are identified by names and parameters so you don't have to manipulate URLs
directly. Routes can be nested, introducing the notion of _route segments_.
- __Route change listeners__
- __Route node change listeners__: you can add listeners to be triggered on a specific named route node. They will be triggered if that named route node is the node a component tree needs to be re-rendered from.
- __Segments deactivation__: you can register components with the router with a `canDeactivate` method. On a route change, it will ask those components if they allow navigation. Similar to _Angular 2_ and _Aurelia_
routers.
- __You are in control!__ You decide what to do on a route change and how to do it.

## API

Expand Down
15 changes: 8 additions & 7 deletions dist/commonjs/router5.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,15 +204,16 @@ var Router5 = (function () {
// Do not proceed further if states are the same and no reload
// (no desactivation and no callbacks)
if (sameStates && !opts.reload) return;

// Transition and amend history
if (!sameStates) {
var canTransition = this._transition(this.lastStateAttempt, this.lastKnownState);
var canTransition = this._transition(this.lastStateAttempt, this.lastKnownState);

if (canTransition) {
window.history[opts.replace ? 'replaceState' : 'pushState'](this.lastStateAttempt, '', this.options.useHash ? '#' + path : path);
// Update lastKnowState
this.lastKnownState = this.lastStateAttempt;
}
if (canTransition) {
// Update lastKnowState
this.lastKnownState = this.lastStateAttempt;
}
if (canTransition && !sameStates) {
window.history[opts.replace ? 'replaceState' : 'pushState'](this.lastStateAttempt, '', this.options.useHash ? '#' + path : path);
}
}
}]);
Expand Down
15 changes: 8 additions & 7 deletions dist/umd/router5.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,15 +211,16 @@
// Do not proceed further if states are the same and no reload
// (no desactivation and no callbacks)
if (sameStates && !opts.reload) return;

// Transition and amend history
if (!sameStates) {
var canTransition = this._transition(this.lastStateAttempt, this.lastKnownState);
var canTransition = this._transition(this.lastStateAttempt, this.lastKnownState);

if (canTransition) {
window.history[opts.replace ? 'replaceState' : 'pushState'](this.lastStateAttempt, '', this.options.useHash ? '#' + path : path);
// Update lastKnowState
this.lastKnownState = this.lastStateAttempt;
}
if (canTransition) {
// Update lastKnowState
this.lastKnownState = this.lastStateAttempt;
}
if (canTransition && !sameStates) {
window.history[opts.replace ? 'replaceState' : 'pushState'](this.lastStateAttempt, '', this.options.useHash ? '#' + path : path);
}
}
}]);
Expand Down
16 changes: 8 additions & 8 deletions modules/Router5.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,16 +157,16 @@ export default class Router5 {
// Do not proceed further if states are the same and no reload
// (no desactivation and no callbacks)
if (sameStates && !opts.reload) return

// Transition and amend history
if (!sameStates) {
let canTransition = this._transition(this.lastStateAttempt, this.lastKnownState)
let canTransition = this._transition(this.lastStateAttempt, this.lastKnownState)

if (canTransition) {
window.history[opts.replace ? 'replaceState' : 'pushState'](this.lastStateAttempt, '', this.options.useHash ? `#${path}` : path)
// Update lastKnowState
this.lastKnownState = this.lastStateAttempt
}
if (canTransition) {
// Update lastKnowState
this.lastKnownState = this.lastStateAttempt
}
if (canTransition && !sameStates) {
window.history[opts.replace ? 'replaceState' : 'pushState'](this.lastStateAttempt, '', this.options.useHash ? `#${path}` : path)
}

}
}
14 changes: 13 additions & 1 deletion tests/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,18 @@ describe('router5', function () {
router.navigate('orders.pending');
expect(window.location.hash).toBe('#/orders/pending');
expect(listeners.global).toHaveBeenCalledWith(router.lastKnownState, previousState);
router.removeListener(listeners.global);
});

it('should invoke listeners on navigation to same state if reload is set to true', function () {
spyOn(listeners, 'global');
router.addListener(listeners.global);

router.navigate('orders.pending');
expect(listeners.global).not.toHaveBeenCalled();

router.navigate('orders.pending', {}, {reload: true});
expect(listeners.global).toHaveBeenCalled();
});

it('should handle popstate events', function () {
Expand All @@ -94,7 +106,7 @@ describe('router5', function () {
router.removeListener(listeners.global);
spyOn(listeners, 'global');

router.navigate('orders.view', {id: 123});
router.navigate('orders.view', {id: 123}, {replace: true});
expect(listeners.global).not.toHaveBeenCalled();
});

Expand Down

0 comments on commit 2317287

Please sign in to comment.