My transitionToRoute method can't trigger the model hook in my route, why?

In my router, I defined one route:

this.route('static', {path: '/static/:title'});

And in my IndexView’s didInsertElement, when I clicked a button, the following code will be executed:

this.get('controller').transitionToRoute('static', {title: 'content'});

And in my StaticRoute:

App.StaticRoute = Em.Route.extend({

    model: function(param) {
        console.log(_Const.url.getStaticPic + '?callback=?&title=' + param.title);
        return $.getJSON(_Const.url.getStaticPic + '?callback=?&title=' + param.title).then(function(data) {
            return data;
        });
    },

    setupController: (controller, model) {
        console.log('in static setupController');
    }
});

When transitionToRoute in the IndexView is called, no info was printed in the console, meaning that the model hook or the setupController hook didn’t get executed in the StaticRoute. Why does this happen? Is there anything I’ve done wrong? Thanks for your help

You pass the model into transitionToRoute so there’s no need to call the model hook. Controller - 4.4 - Ember API Documentation

In your case you should change

this.get('controller').transitionToRoute('static', {title: 'content'});

to

this.get('controller').transitionToRoute('static', model);

It seems you can also do what you want more directly using transitionTo. This will let you do

transitionTo("/static/" + title);

it’s available in routes, not sure about controllers. If it’s not available in controllers you can always handle your action in the route instead of the controller and do the transition from there.

1 Like
Thanks! I'll try the 2nd way you told me, I just want to trigger the model hook in the route