Search code examples
ember.jssocket.ioember-cliengine.io

Ember.js - How can I transition to another route from an initialiser?


I am using an initialiser to connect to a socket server. After a set amount of time I would like to redirect to another route (which handles the error).

How can I do this from the initialiser?

Current Code Below:

/* Socket Initializer */
export default {
    name:   'Socket',
    initialize: function(Container, Application) {

        Application.deferReadiness();       

        var IO = Container.lookup('socket:main');

        IO.connect({
            reconnectionDelayMax: 1000
        });


        IO.socket.on('connect', function(){
            Application.advanceReadiness();
        });


        var RetryCounter = 0;

        IO.socket.on('connect_error', function(Error) {

            if (RetryCounter == 2) {


                //this.transitionTo('development');
                //Application.transitionTo('development');
                //Container.transitionTo('development');

                // None of the above work.

            }
            RetryCounter++;  
        });
    }
};

Solution

  • You can grab the router from the container and call transitionTo from it.

    var router = container.lookup('router:main');
    router.transitionTo('foo');
    

    Contrived Example: http://emberjs.jsbin.com/bukuvuho/3/edit