Skip to content
This repository has been archived by the owner on Sep 20, 2020. It is now read-only.

Commit

Permalink
fix(previous): Previous state now tracked using $transition$ promise
Browse files Browse the repository at this point in the history
 - This allows previous state to properly manage transition cancelled and redirected

 Closes #120
  • Loading branch information
christopherthielen committed Jan 31, 2015
1 parent 0722e26 commit 1127ef6
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 17 deletions.
1 change: 1 addition & 0 deletions files.js
Expand Up @@ -22,6 +22,7 @@ modules.sticky.src = ['src/stickyProvider.js'].concat(modules.sticky.src);
modules.core.src.push('src/util.js');
modules.future.test.push('src/fsfactories/ngload.js');
modules.future.test.push('src/fsfactories/iframe.js');
modules.previous.test.push('build/modular/ct-ui-router-extras.transition.js');

// Build the monolithic module 'all' which sucks in all the others
modules.all = {
Expand Down
24 changes: 7 additions & 17 deletions src/previous.js
@@ -1,25 +1,15 @@
angular.module('ct.ui.router.extras.previous', [ 'ct.ui.router.extras.core' ]).service("$previousState",
angular.module('ct.ui.router.extras.previous', [ 'ct.ui.router.extras.core', 'ct.ui.router.extras.transition' ]).service("$previousState",
[ '$rootScope', '$state',
function ($rootScope, $state) {
var previous = null;
var memos = {};
var previous = null, lastPrevious = null, memos = {};

var lastPrevious = null;

$rootScope.$on("$stateChangeStart", function (evt, toState, toStateParams, fromState, fromStateParams) {
// State change is starting. Keep track of the CURRENT previous state in case we have to restore it
$rootScope.$on("$transitionStart", function(evt, $transition$) {
lastPrevious = previous;
previous = { state: fromState, params: fromStateParams };
});

$rootScope.$on("$stateChangeError", function () {
// State change did not occur due to an error. Restore the previous previous state.
previous = lastPrevious;
lastPrevious = null;
});
previous = $transition$.from;

$rootScope.$on("$stateChangeSuccess", function () {
lastPrevious = null;
$transition$.promise.then(commit).catch(revert);
function commit() { lastPrevious = null; }
function revert() { previous = lastPrevious; }
});

var $previousState = {
Expand Down
12 changes: 12 additions & 0 deletions test/previousSpec.js
Expand Up @@ -61,6 +61,18 @@ describe("$previousState", function () {
$q.flush();
expect($state.current.name === "tabs.tabs2");
});

// Test for #120
it("should go to previous state after another transition is cancelled", inject(function($rootScope) {
testGo("top", { entered: 'top' });
testGo("top.people.managerlist", { entered: ['top.people', 'top.people.managerlist'] });

var transitionNum = 0;
$rootScope.$on("$stateChangeStart", function(evt) { if (transitionNum++ === 0) { evt.preventDefault(); } });

testGo("top.inv.storelist", undefined, { redirect: 'top.people.managerlist'}); // Cancelled, so we're still at original state
expect($previousState.get().state.name).toBe("top");
}));
});

describe('.memo()', function () {
Expand Down

0 comments on commit 1127ef6

Please sign in to comment.