Skip to content

Commit cba5233

Browse files
author
Jim Anders
committed
Fixes #125
Add reload on search proprty to state config. Signed-off-by: Jim Anders <[email protected]> Fixes #125 Add reload on search proprty to state config. Signed-off-by: Jim Anders <[email protected]> Extract shouldTiggerReload Function
1 parent 70a3c30 commit cba5233

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

src/state.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -336,8 +336,9 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory, $
336336
// But clear 'transition', as we still want to cancel any other pending transitions.
337337
// TODO: We may not want to bump 'transition' if we're called from a location change that we've initiated ourselves,
338338
// because we might accidentally abort a legitimate transition initiated from code?
339-
if (to === from && locals === from.locals && !options.reload) {
340-
syncUrl();
339+
if (shouldTriggerReload(to, from, locals, options) ) {
340+
if ( to.self.reloadOnSearch !== false )
341+
syncUrl();
341342
$state.transition = null;
342343
return $q.when($state.current);
343344
}
@@ -577,6 +578,12 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory, $
577578
});
578579
return filtered;
579580
}
581+
582+
function shouldTriggerReload(to, from, locals, options) {
583+
if ( to === from && ((locals === from.locals && !options.reload) || (to.self.reloadOnSearch === false)) ) {
584+
return true;
585+
}
586+
}
580587
}
581588

582589
angular.module('ui.router.state')

test/stateSpec.js

+16-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ describe('state', function () {
2626
H = { data: {propA: 'propA', propB: 'propB'} },
2727
HH = { parent: H },
2828
HHH = {parent: HH, data: {propA: 'overriddenA', propC: 'propC'} },
29+
RS = { url: '^/search?term', reloadOnSearch: false },
2930
AppInjectable = {};
3031

3132
beforeEach(module(function ($stateProvider, $provide) {
@@ -45,6 +46,7 @@ describe('state', function () {
4546
.state('H', H)
4647
.state('HH', HH)
4748
.state('HHH', HHH)
49+
.state('RS', RS)
4850

4951
.state('home', { url: "/" })
5052
.state('home.item', { url: "front/:id" })
@@ -141,6 +143,18 @@ describe('state', function () {
141143
expect($state.current).toBe(A);
142144
}));
143145

146+
it('doesn\'t trigger state change if reloadOnSearch is false', inject(function ($state, $q, $location, $rootScope){
147+
initStateTo(RS);
148+
$location.search({term: 'hello'});
149+
var called;
150+
$rootScope.$on('$stateChangeStart', function (ev, to, toParams, from, fromParams) {
151+
called = true
152+
});
153+
$q.flush();
154+
expect($location.search()).toEqual({term: 'hello'});
155+
expect(called).toBeFalsy();
156+
}));
157+
144158
it('ignores non-applicable state parameters', inject(function ($state, $q) {
145159
$state.transitionTo('A', { w00t: 'hi mom!' });
146160
$q.flush();
@@ -641,6 +655,7 @@ describe('state', function () {
641655
'H',
642656
'HH',
643657
'HHH',
658+
'RS',
644659
'about',
645660
'about.person',
646661
'about.person.item',
@@ -899,4 +914,4 @@ describe('state queue', function(){
899914
expect(list.map(function(state) { return state.name; })).toEqual(expectedStates);
900915
});
901916
});
902-
});
917+
});

0 commit comments

Comments
 (0)