Skip to content

Commit 1303697

Browse files
fix($transition) get a single transition to run.
- add Path.reverse to reverse a path inline (like Array.reverse) - add "State X is already defined" to StateQueueManager.register() and flush() - hook up Transition.run() in $state.transitionTo - copied state.to/state.from to toState/fromState. meh - fix bug in entering-states code - init _fromPath in $transition.init() - fix transitionSpec to not try to clobber the root state ('')
1 parent 0fb073d commit 1303697

File tree

4 files changed

+25
-19
lines changed

4 files changed

+25
-19
lines changed

src/resolve.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,10 @@ function $Resolve( $q, $injector) {
236236
slice: function(start, end) {
237237
return new Path(elements.slice(start, end));
238238
},
239+
reverse: function() {
240+
elements.reverse();
241+
return self;
242+
},
239243
states: function() {
240244
return pluck(elements, "state");
241245
},

src/state.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,16 @@ function StateQueueManager(states, builder, $urlRouterProvider) {
5050
extend(this, {
5151
register: function(config, pre) {
5252
// Wrap a new object around the state so we can store our private details easily.
53-
state = inherit(config, {
53+
var state = inherit(config, {
5454
// name: builder.name(config),
5555
self: config,
5656
resolve: config.resolve || {},
5757
toString: function() { return this.name; }
5858
});
5959

6060
if (!isString(state.name)) throw new Error("State must have a valid name");
61-
// if (registered.hasOwnProperty(name)) throw new Error("State '" + name + "'' is already defined");
61+
if (states[state.name] || pluck(queue, 'name').indexOf(state.name) !== -1)
62+
throw new Error("State '" + state.name + "' is already defined");
6263
if (pre)
6364
queue.unshift(state);
6465
else
@@ -75,6 +76,8 @@ function StateQueueManager(states, builder, $urlRouterProvider) {
7576
orphanIdx = orphans.indexOf(state);
7677

7778
if (result) {
79+
if (states[state.name] !== undefined)
80+
throw new Error("State '" + name + "' is already defined");
7881
states[state.name] = state;
7982
this.attachRoute(state);
8083
if (orphanIdx >= 0) orphans.splice(orphanIdx, 1);
@@ -918,7 +921,7 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactoryProvider) {
918921
// and return a promise for the new state. We also keep track of what the
919922
// current promise is, so that we can detect overlapping transitions and
920923
// keep only the outcome of the last transition.
921-
var current = transition.run()
924+
var current = transition.runAsync()
922925
.then(function(data) {
923926
console.log("hur", data);
924927
transitionSuccess();

src/transition.js

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -77,20 +77,20 @@ function $TransitionProvider() {
7777
* @returns {Object} New `Transition` object
7878
*/
7979
function Transition(fromState, fromParams, toState, toParams, options) {
80-
var transition = this;
81-
var keep = 0, state, retained, entering, exiting;
82-
var hasRun = false, hasCalculated = false;
83-
80+
var transition = this; // Transition() object
8481
// grab $transition's current path
85-
var toPath, fromPath = _fromPath;
82+
var toPath, fromPath = _fromPath; // Path() objects
83+
var retained, entering, exiting; // Path() objects
84+
var keep = 0, state, hasRun = false, hasCalculated = false;
8685

8786
var states = {
8887
to: stateMatcher(toState, options),
8988
from: stateMatcher(fromState, options)
9089
};
90+
toState = states.to; fromState = states.from;
9191

9292
function isTargetStateValid() {
93-
var state = stateMatcher(toState, options);
93+
var state = states.to;
9494

9595
if (!isDefined(state)) {
9696
if (!options || !options.relative) return "No such state " + angular.toJson(toState);
@@ -148,9 +148,8 @@ function $TransitionProvider() {
148148
// - resolve PathElement lazy resolvables
149149
// - then, invokeAsync onEnter
150150

151-
var exitingElements = transition.exiting().slice(0);
152-
exitingElements.reverse();
153-
var enteringElements = transition.entering();
151+
var exitingElements = transition.exiting().slice(0).reverse().elements;
152+
var enteringElements = transition.entering().elements;
154153
var promiseChain = $q.when(true);
155154
forEach(exitingElements, function(elem) {
156155
if (elem.state.onExit) {
@@ -159,7 +158,7 @@ function $TransitionProvider() {
159158
}
160159
});
161160
forEach(enteringElements, function(elem) {
162-
var resolveContext = fromPath.resolveContext(elem);
161+
var resolveContext = toPath.resolveContext(elem);
163162
promiseChain.then(function() { return elem.resolve(resolveContext, { policy: "lazy" }); });
164163
if (elem.state.onEnter) {
165164
var nextStep = transitionStep(elem.state.onEnter, resolveContext);
@@ -310,7 +309,8 @@ function $TransitionProvider() {
310309
return (toState === fromState && !options.reload);
311310
},
312311
runAsync: function() {
313-
var pathContext = new PathContext(toPath);
312+
calculateTreeChanges();
313+
var pathContext = new ResolveContext(toPath);
314314
return toPath.resolve(pathContext, { policy: "eager" })
315315
.then( buildTransitionSteps );
316316
},
@@ -346,6 +346,7 @@ function $TransitionProvider() {
346346

347347

348348
$transition.init = function init(state, params, matcher) {
349+
_fromPath = new Path(state.path);
349350
from = { state: state, params: params };
350351
to = { state: null, params: null };
351352
stateMatcher = matcher;

test/transitionSpec.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,14 @@ describe('$transition:', function () {
5252
var substates = omit.apply(null, [state].concat(stateProps));
5353

5454
thisState.name = name;
55-
// if (parent && parent.name) {
56-
// thisState.name = parent.name + "." + name;
57-
// }
58-
thisState.parent = parent;
55+
thisState.parent = parent.name;
5956
thisState.data = { children: [] };
6057

6158
angular.forEach(substates, function (value, key) {
6259
thisState.data.children.push(loadStates(thisState, value, key));
6360
});
64-
statesMap[name] = thisState;
61+
if (name)
62+
statesMap[name] = thisState;
6563
return thisState;
6664
}
6765

0 commit comments

Comments
 (0)