Skip to content

Commit ca3b477

Browse files
committed
feat($state): implementing $state.reload()
Closes #76, #526.
1 parent 218bdf2 commit ca3b477

File tree

2 files changed

+36
-9
lines changed

2 files changed

+36
-9
lines changed

src/state.js

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,6 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory, $
147147
if (!queue[parentName]) {
148148
queue[parentName] = [];
149149
}
150-
151150
queue[parentName].push(state);
152151
}
153152

@@ -164,8 +163,7 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory, $
164163
if (states.hasOwnProperty(name)) throw new Error("State '" + name + "'' is already defined");
165164

166165
// Get parent name
167-
var parentName =
168-
(name.indexOf('.') !== -1) ? name.substring(0, name.lastIndexOf('.'))
166+
var parentName = (name.indexOf('.') !== -1) ? name.substring(0, name.lastIndexOf('.'))
169167
: (isString(state.parent)) ? state.parent
170168
: '';
171169

@@ -265,14 +263,19 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory, $
265263
transition: null
266264
};
267265

266+
$state.reload = function reload() {
267+
$state.transitionTo($state.current, $stateParams, { reload: true, inherit: false, notify: false });
268+
};
269+
268270
$state.go = function go(to, params, options) {
269271
return this.transitionTo(to, params, extend({ inherit: true, relative: $state.$current }, options));
270272
};
271273

272274
$state.transitionTo = function transitionTo(to, toParams, options) {
273-
if (!isDefined(options)) options = (options === true || options === false) ? { location: options } : {};
274275
toParams = toParams || {};
275-
options = extend({ location: true, inherit: false, relative: null, notify: true, $retry: false }, options);
276+
options = extend({
277+
location: true, inherit: false, relative: null, notify: true, reload: false, $retry: false
278+
}, options || {});
276279

277280
var from = $state.$current, fromParams = $state.params, fromPath = from.path;
278281
var evt, toState = findState(to, options.relative);
@@ -297,8 +300,7 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory, $
297300
if (retryTransition !== $state.transition) return TransitionSuperseded;
298301
redirect.options.$retry = true;
299302
return $state.transitionTo(redirect.to, redirect.toParams, redirect.options);
300-
},
301-
function() {
303+
}, function() {
302304
return TransitionAborted;
303305
});
304306
syncUrl();
@@ -325,7 +327,7 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory, $
325327
// Starting from the root of the path, keep all levels that haven't changed
326328
var keep, state, locals = root.locals, toLocals = [];
327329
for (keep = 0, state = toPath[keep];
328-
state && state === fromPath[keep] && equalForKeys(toParams, fromParams, state.ownParams);
330+
state && state === fromPath[keep] && equalForKeys(toParams, fromParams, state.ownParams) && !options.reload;
329331
keep++, state = toPath[keep]) {
330332
locals = toLocals[keep] = state.locals;
331333
}
@@ -334,7 +336,7 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory, $
334336
// But clear 'transition', as we still want to cancel any other pending transitions.
335337
// TODO: We may not want to bump 'transition' if we're called from a location change that we've initiated ourselves,
336338
// because we might accidentally abort a legitimate transition initiated from code?
337-
if (to === from && locals === from.locals) {
339+
if (to === from && locals === from.locals && !options.reload) {
338340
syncUrl();
339341
$state.transition = null;
340342
return $q.when($state.current);

test/stateSpec.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,14 @@ describe('state', function () {
8181
}
8282
}
8383
})
84+
.state('resolveTimeout', {
85+
url: "/:foo",
86+
resolve: {
87+
value: function ($timeout) {
88+
return $timeout(function() { log += "Success!"; }, 1);
89+
}
90+
}
91+
})
8492

8593
.state('first', { url: '^/first/subpath' })
8694
.state('second', { url: '^/second' });
@@ -455,6 +463,22 @@ describe('state', function () {
455463
}));
456464
});
457465

466+
describe('.reload()', function () {
467+
it('should reload the current state with the current parameters', inject(function ($state, $q, $timeout) {
468+
$state.transitionTo('resolveTimeout', { foo: "bar" });
469+
$q.flush();
470+
expect(log).toBe('');
471+
472+
$timeout.flush();
473+
expect(log).toBe('Success!');
474+
475+
$state.reload();
476+
$q.flush();
477+
$timeout.flush();
478+
expect(log).toBe('Success!Success!');
479+
}));
480+
});
481+
458482
describe('.is()', function () {
459483
it('should return true when the current state is passed', inject(function ($state, $q) {
460484
$state.transitionTo(A); $q.flush();
@@ -623,6 +647,7 @@ describe('state', function () {
623647
'home.item',
624648
'home.redirect',
625649
'resolveFail',
650+
'resolveTimeout',
626651
'second'
627652
];
628653
expect(list.map(function(state) { return state.name; })).toEqual(names);

0 commit comments

Comments
 (0)