From 331557dc5816f8079854206ef63380bc06d74c32 Mon Sep 17 00:00:00 2001 From: James Homer Date: Wed, 23 Jul 2014 18:01:53 +0100 Subject: [PATCH] feat($state): expose previous state/params via $state --- src/state.js | 9 +++++++++ test/stateSpec.js | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/src/state.js b/src/state.js index 1986b1b22..586dd6675 100644 --- a/src/state.js +++ b/src/state.js @@ -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. * @@ -615,6 +618,9 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory) { params: {}, current: root.self, $current: root, + previous: root.self, + $previous: root, + previousParams: {}, transition: null }; @@ -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; diff --git a/test/stateSpec.js b/test/stateSpec.js index 75e11f268..33bed9591 100644 --- a/test/stateSpec.js +++ b/test/stateSpec.js @@ -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(); @@ -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(); @@ -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();