Skip to content

feat($state): expose previous state/params via $state #1218

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
9 changes: 9 additions & 0 deletions src/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,9 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory) {
* you'd like to test against the current active state.
* @property {object} current A reference to the state's config object. However
* you passed it in. Useful for accessing custom data.
* @property {object} previous A reference to the previous state's config object.
* @property {object} previousParams A param object, e.g. {sectionId: section.id)},
* represents the param's of the previous state.
* @property {object} transition Currently pending transition. A promise that'll
* resolve or reject.
*
Expand Down Expand Up @@ -615,6 +618,9 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory) {
params: {},
current: root.self,
$current: root,
previous: root.self,
$previous: root,
previousParams: {},
transition: null
};

Expand Down Expand Up @@ -900,6 +906,9 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory) {
$state.$current = to;
$state.current = to.self;
$state.params = toParams;
$state.$previous = from;
$state.previous = from.self;
$state.previousParams = fromParams;
copy($state.params, $stateParams);
$state.transition = null;

Expand Down
43 changes: 43 additions & 0 deletions test/stateSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,20 @@ describe('state', function () {
});


describe('.previous', function () {
it('is always defined', inject(function ($state) {
expect($state.previous).toBeDefined();
}));

it('updates asynchronously as the transitionTo() promise is resolved', inject(function ($state, $q) {
initStateTo(A);
var trans = $state.transitionTo(B, {});
$q.flush();
expect($state.previous).toBe(A);
}));
});


describe('$current', function () {
it('is always defined', inject(function ($state) {
expect($state.$current).toBeDefined();
Expand All @@ -598,6 +612,20 @@ describe('state', function () {
});


describe('$previous', function () {
it('is always defined', inject(function ($state) {
expect($state.$previous).toBeDefined();
}));

it('wraps the raw state object', inject(function ($state, $q) {
initStateTo(A);
var trans = $state.transitionTo(B, {});
$q.flush();
expect($state.$previous.data).toBe(A.data); // 'data' is reserved for app use
}));
});


describe('.params', function () {
it('is always defined', inject(function ($state) {
expect($state.params).toBeDefined();
Expand All @@ -611,6 +639,21 @@ describe('state', function () {
});


describe('.previousParams', function () {
it('is always defined', inject(function ($state) {
expect($state.previousParams).toBeDefined();
expect(angular.isObject($state.previousParams)).toBe(true);
}));

it('contains the parameter values for the previous state', inject(function ($state, $q) {
initStateTo(D, { x: 'x value', z: 'invalid value' });
var trans = $state.transitionTo(A, {});
$q.flush();
expect($state.previousParams).toEqual({ x: 'x value', y: undefined });
}));
});


describe('.transition', function () {
it('is null when no transition is taking place', inject(function ($state, $q) {
expect($state.transition).toBeNull();
Expand Down