Skip to content

Commit 417336b

Browse files
author
Maor Yosef
committed
feat($state): refactor code to fit ui-router standarts
1 parent 3d07567 commit 417336b

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

src/state.js

+11-5
Original file line numberDiff line numberDiff line change
@@ -1000,20 +1000,26 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory) {
10001000
// Starting from the root of the path, keep all levels that haven't changed
10011001
var keep = 0, state = toPath[keep], locals = root.locals, toLocals = [];
10021002
var skipTriggerReloadCheck = false;
1003+
10031004
if (!options.reload) {
10041005
while (state && state === fromPath[keep] && state.ownParams.$$equals(toParams, fromParams)) {
10051006
locals = toLocals[keep] = state.locals;
10061007
keep++;
10071008
state = toPath[keep];
10081009
}
10091010
} else if (isString(options.reload) || isObject(options.reload)) {
1010-
skipTriggerReloadCheck = true;
1011-
var stateName = isString(options.reload) ? options.reload : options.reload.name;
1012-
if (!isDefined(findState(stateName))) {
1013-
throw new Error("No such state '" + stateName + "'");
1011+
if (isObject(options.reload) && !options.reload.name) {
1012+
throw new Error('Invalid reload state object');
1013+
}
1014+
1015+
var reloadState = options.reload === true ? fromPath[0] : findState(options.reload);
1016+
if (options.reload && !reloadState) {
1017+
throw new Error("No such reload state '" + (isString(options.reload) ? options.reload : options.reload.name) + "'");
10141018
}
10151019

1016-
while (state && state === fromPath[keep] && state.toString().toLowerCase() !== stateName.toLowerCase()) {
1020+
skipTriggerReloadCheck = true;
1021+
1022+
while (state && state === fromPath[keep] && state !== reloadState) {
10171023
locals = toLocals[keep] = state.locals;
10181024
keep++;
10191025
state = toPath[keep];

test/stateSpec.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -623,7 +623,7 @@ describe('state', function () {
623623

624624
expect(function(){
625625
$state.reload('logInvalid')}
626-
).toThrow("No such state 'logInvalid'");
626+
).toThrow("No such reload state 'logInvalid'");
627627
}));
628628

629629
it('should throw an exception for invalid reload state object', inject(function ($state, $q, $timeout, $rootScope, $compile) {
@@ -633,8 +633,12 @@ describe('state', function () {
633633
expect(log).toBe('logA;logB;logC;');
634634

635635
expect(function(){
636-
$state.reload({})}
637-
).toThrow("No such state 'undefined'");
636+
$state.reload({foo:'bar'})}
637+
).toThrow("Invalid reload state object");
638+
639+
expect(function(){
640+
$state.reload({name:'invalidState'})}
641+
).toThrow("No such reload state 'invalidState'");
638642
}));
639643
});
640644

0 commit comments

Comments
 (0)