Skip to content

Added a .back method to ui-router, fixes #92 #861

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

Closed
wants to merge 1 commit into from
Closed
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
30 changes: 30 additions & 0 deletions src/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,8 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory, $
params: {},
current: root.self,
$current: root,
previous: undefined,
previousParams: undefined,
transition: null
};

Expand Down Expand Up @@ -533,6 +535,32 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory, $
return this.transitionTo(to, params, extend({ inherit: true, relative: $state.$current }, options));
};

/**
* @ngdoc function
* @name ui.router.state.$state#back
* @methodOf ui.router.state.$state
*
* @description
* Convenience method for transitioning back to previous state. `$state.back` calls
* `$state.transitionTo` internally.
*
* @example
* <pre>
* var app = angular.module('app', ['ui.router.state']);
*
* app.controller('ctrl', function ($scope, $state) {
* $scope.changeState = function () {
* $state.back();
* };
* });
* </pre>
*
* @param {object} options If Object is passed, object is an options hash.
*/
$state.back = function (options) {
return this.transitionTo($state.previous, $state.previousParams, options);
};

/**
* @ngdoc function
* @name ui.router.state.$state#transitionTo
Expand Down Expand Up @@ -686,6 +714,8 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory, $
if ($state.transition !== transition) return TransitionSuperseded;

// Update globals in $state
$state.previous = $state.current;
$state.previousParams = $state.params;
$state.$current = to;
$state.current = to.self;
$state.params = toParams;
Expand Down
20 changes: 20 additions & 0 deletions test/stateSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,26 @@ describe('state', function () {
}));
});

describe('.back()', function () {
it('transitions to previous state', inject(function ($state, $q) {
$state.transitionTo('about.person.item'); $q.flush();
$state.go('^.^.sidebar'); $q.flush();
$state.back(); $q.flush();

expect($state.$current.name).toBe('about.person.item');

}));

it('transitions to previous state', inject(function ($state, $stateParams, $q) {
$state.transitionTo('about.person.item', { id:5 }); $q.flush();
$state.go('^.^.sidebar'); $q.flush();
$state.back(); $q.flush();

expect($stateParams).toEqual({ person: '', id: '5' });
}));

});

describe('.reload()', function () {
it('should reload the current state with the current parameters', inject(function ($state, $q, $timeout) {
$state.transitionTo('resolveTimeout', { foo: "bar" });
Expand Down