EdgifyJP
EdgifyJP

Reputation: 93

Ember router with transient states

Inspired by Emberjs: Conditional redirect in router I thought I could use transient states in Ember router, as that is what the 'index' route in that question is - it is entered and immediately transitions to another state without a new event triggering it.

I can get the desired effect using the following code (action triggering transitionTwice), however the URL is updated in reverse order leaving it at the first state even though the App traverses through the states in the correct order ending in the expected last state.

App.Router = Ember.Router.extend({
    enableLogging: true,

    root: Ember.Route.extend({

        transitionTwice:  Ember.Route.transitionTo('twiceStep1'),

        index: Ember.Route.extend({
            route: '/',
        }),

        twiceStep1: Ember.Route.extend({
            route: '/twiceStep1',

            connectOutlets: function (router) {
                router.transitionTo('twiceStep2');
            }
        }),
        twiceStep2: Ember.Route.extend({
            route: '/twiceStep2',
        })
    })
}),
App.initialize();

The console output shows the correct state changes:

STATEMANAGER: Sending event 'transitionTwice' to state root.
STATEMANAGER: Entering root.twiceStep1
STATEMANAGER: Entering root.twiceStep2

However the URL ends up in:

...test2.html#/twiceStep1

Can anyone spot anything I am doing wrong?

Thanks, Jon.

Upvotes: 1

Views: 275

Answers (1)

EdgifyJP
EdgifyJP

Reputation: 93

I have been able to get this to work successfully by wrapping the second transitionTo() call in Ember.run.next().

Ember.run.next(function () {
    router.transitionTo('twiceStep2');
});

The problem is that for any transition the URL is updated after the connectOutlets() call completes. This means that chained calls to transitionTo from connectOutlets nest and the URL is updated in reverse order as the calls complete.

While this fixes my immediate problem I'm not sure this really constitutes a final answer to the question as this implies that the final state needs to have the knowledge that it will be entered from a previous, transient state - which isn't good separation. But I thought I should record it in case anyone else struggles with this anomaly.

Upvotes: 1

Related Questions