Search code examples
ember.jsember-router

Is there is any hook like "didInsertElement" in router?


Using third party framework, I want set selected value.

Is there is any hook after view inserted into DOM in ember.js new router?


Solution

  • Agreed with Karl above. However, maybe you've just asked the question in a bad way. In the new router, you have the setupController, which is invoked when Ember moves into that route. So for example, if you move into /#/dashboard, then DashboardController, DashboardView, DashboardRoute will all be initialised.

    Aside from the fact that you could use the didInsertElement on the DashboardView at this point, you have the setupController method which you can overwrite in the DashboardRoute. In here you can set-up the controller, and perhaps do whatever it is you're trying to do:

    (The setupController will only be invoked when you enter the route, but the view won't have rendered by the time you're moving into it. For that you'll need didInsertElement and that's that. setupController is for setting up the controller, which can be thought of as an ever-persistent singleton.)

    var DashboardRoute = Ember.Route.extend({
        setupController: function(controller) {
            // We're in the route, so let's do something now.
            controller.set('myText', 'Add this to the controller!');
        }
    });