Model and setupController hooks not visited on transitionTo

Hello,

I have noticed that when I switch to a specific route using transitionTo the model and setupController hooks are not being triggered when transitioning to that route. why is that? When I use link-to the transition happens and the hooks are triggered

Note that I am using ember 1.13

Note that I am not using ember-data because of some back-end set up affecting the structure of my JSON.

Here is the code in router.js

export default Router.map(function() {
    this.resource('users', function() {
	this.route('new');
    });
	
    this.resource('user', { path: 'user/:user_id' }, function() {
    });
});

in /templates/user.hbs using link-to works perfectly and model and setupController hooks in /routes/users.js are triggered:

<p>{{#link-to 'user' user}}View profile{{/link-to}}</p>

in /routes/users/user.js, using transitionTo loads the correct template and display data previously fetched, but by using console.log() I have found out that model and setupController hooks inside /routes/users.js are not triggered. Why? Also, I am not using ember-data(no mode or store.find(), store.findAll()) so where is the displayed data coming from? I am not using ember-data and will not be creating my own serializer. Not because I don’t want or like them, but based on the way the API is built and the requirements it is best to implement things this way.

actions: {
    createUser: function() {
	var t = this;
	Ember.$.ajax({
	    url: t.apiUrl("AdminCreateUser"),
	    data: {
	        newUsername: Ember.$("#userName").val();,
	    },
	    dataType: 'json',
	    type: 'POST',
	    async: false,
	    success: function(data) {
	        if (data.success) {
	            t.transitionTo('users');
		} else {
		    t.showMessage("Failed");
		}
	     },
	    error: function(data) {
    	         }
	});
   }
} 

Code in routes/users.js

model: function(params) {
	console.log("usersjs model");
},
setupController: function(controller) {
    console.log("usersjs setup");
    var users = {};        
    Ember.$.ajax({
        url: this.apiUrl("AdminUserList"),
        data: {
            token: "",
        },
        dataType: 'json',
        type: 'POST',
        async: false,
        success: function(data) {
            if (data.success) {
            	users = data.p.users;
            }
        },
        error: function(data) {
            // if error occured
        }
    });
    controller.set('users', users);
}

After more research, I have noticed that the above mentioned hooks are triggered when using link-to or transitionTo from a different route, but aren’t when going from a child to its parent.

Try this instead:

<p>{{#link-to 'user' user.id}}View profile{{/link-to}}</p>

If you pass in an actual object, then the route won’t fire its model hooks since it already has its model.

1 Like