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 no longer tracks abstract from-states (…
Browse files Browse the repository at this point in the history
…e.g., root)

 Closes #123
  • Loading branch information
christopherthielen committed Jan 31, 2015
1 parent 1127ef6 commit b0431d6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
10 changes: 8 additions & 2 deletions src/previous.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,14 @@ angular.module('ct.ui.router.extras.previous', [ 'ct.ui.router.extras.core', 'ct
var previous = null, lastPrevious = null, memos = {};

$rootScope.$on("$transitionStart", function(evt, $transition$) {
lastPrevious = previous;
previous = $transition$.from;
var from = $transition$.from;
// Check if the fromState is navigable before tracking it.
// Root state doesn't get decorated with $$state(). Doh.
var fromState = from.state && from.state.$$state && from.state.$$state();
if (fromState && fromState.navigable) {
lastPrevious = previous;
previous = $transition$.from;
}

$transition$.promise.then(commit).catch(revert);
function commit() { lastPrevious = null; }
Expand Down
16 changes: 13 additions & 3 deletions test/previousSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,26 +53,36 @@ describe("$previousState", function () {
$previousState = $get('$previousState');
}));

// Test for #123
it("should not capture root state (or non-navigable states)", inject(function($rootScope) {
testGo("top", { entered: 'top' });
expect($previousState.get()).toBeNull();
}));


describe('.go()', function () {
it("should transition back to the previous state", function () {
testGo("top.people.managerlist", { entered: pathFrom('top', 'top.people.managerlist') });
testGo("top.inv.storelist", { entered: pathFrom('top.inv', 'top.inv.storelist') , exited: pathFrom('top.people.managerlist', 'top.people') });
testGo("top.inv.storelist", { entered: pathFrom('top.inv', 'top.inv.storelist'), exited: pathFrom('top.people.managerlist', 'top.people') });
$previousState.go();
$q.flush();
expect($state.current.name === "tabs.tabs2");
});
});

describe('.get()', function() {
// Test for #120
it("should go to previous state after another transition is cancelled", inject(function($rootScope) {
it("should not return current state, after a 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 b0431d6

Please sign in to comment.