Skip to content

Fixes #125 Add reloadOnSearch property to stateConfig #593

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

Merged
merged 1 commit into from
Nov 23, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
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
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);
});
});
});
});