Skip to content

Commit

Permalink
Fixes #125
Browse files Browse the repository at this point in the history
Add reload on search proprty to state config.

Signed-off-by: Jim Anders <github@janders223.com>

Fixes #125

Add reload on search proprty to state config.

Signed-off-by: Jim Anders <github@janders223.com>

Extract shouldTiggerReload Function
  • Loading branch information
Jim Anders committed Nov 23, 2013
1 parent 70a3c30 commit cba5233
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
11 changes: 9 additions & 2 deletions src/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -336,8 +336,9 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory, $
// But clear 'transition', as we still want to cancel any other pending transitions.
// TODO: We may not want to bump 'transition' if we're called from a location change that we've initiated ourselves,
// because we might accidentally abort a legitimate transition initiated from code?
if (to === from && locals === from.locals && !options.reload) {
syncUrl();
if (shouldTriggerReload(to, from, locals, options) ) {
if ( to.self.reloadOnSearch !== false )
syncUrl();
$state.transition = null;
return $q.when($state.current);
}
Expand Down Expand Up @@ -577,6 +578,12 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory, $
});
return filtered;
}

function shouldTriggerReload(to, from, locals, options) {
if ( to === from && ((locals === from.locals && !options.reload) || (to.self.reloadOnSearch === false)) ) {
return true;
}
}
}

angular.module('ui.router.state')
Expand Down
17 changes: 16 additions & 1 deletion test/stateSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ describe('state', function () {
H = { data: {propA: 'propA', propB: 'propB'} },
HH = { parent: H },
HHH = {parent: HH, data: {propA: 'overriddenA', propC: 'propC'} },
RS = { url: '^/search?term', reloadOnSearch: false },
AppInjectable = {};

beforeEach(module(function ($stateProvider, $provide) {
Expand All @@ -45,6 +46,7 @@ describe('state', function () {
.state('H', H)
.state('HH', HH)
.state('HHH', HHH)
.state('RS', RS)

.state('home', { url: "/" })
.state('home.item', { url: "front/:id" })
Expand Down Expand Up @@ -141,6 +143,18 @@ describe('state', function () {
expect($state.current).toBe(A);
}));

it('doesn\'t trigger state change if reloadOnSearch is false', inject(function ($state, $q, $location, $rootScope){
initStateTo(RS);
$location.search({term: 'hello'});
var called;
$rootScope.$on('$stateChangeStart', function (ev, to, toParams, from, fromParams) {
called = true
});
$q.flush();
expect($location.search()).toEqual({term: 'hello'});
expect(called).toBeFalsy();
}));

it('ignores non-applicable state parameters', inject(function ($state, $q) {
$state.transitionTo('A', { w00t: 'hi mom!' });
$q.flush();
Expand Down Expand Up @@ -641,6 +655,7 @@ describe('state', function () {
'H',
'HH',
'HHH',
'RS',
'about',
'about.person',
'about.person.item',
Expand Down Expand Up @@ -899,4 +914,4 @@ describe('state queue', function(){
expect(list.map(function(state) { return state.name; })).toEqual(expectedStates);
});
});
});
});

0 comments on commit cba5233

Please sign in to comment.