2
votes

I am returning some static json from my ajax call for test purpose before the bakend is ready. Nut when I use transitionToRoute from some function I can see the model hook of the route is not always called. I guess it is caching the static json and I see the route rendering properly. But I am also setting some other properties of the controller in the setUpController hook which also doesn't get executed when the model hook is not called.

This variable needs to set whenever I am changing to this route. If setUpController is not the place to set it where should I set it . So it doesn't fail to get set when ember doesn't call model hook as part of caching process.

    setupController : function(controller, model ) {


        controller.set('isEditing',false);
        controller.set('messages', model.messages);
        controller.set('params', this.get('params'));


        console.log('Set Up controller' );


    },  


    model: function( routeParams) {


        this.set('params',routeParams);
        // return data omitted code

        });


   }

So the isEdiding field doesn't get set when model hook is bypassed. One get around solution is set it before transitioning like this

this.controllerFor("messages").set('isEditing',false);
// then do tranisitioning

Is there any better way to acheive the same thing? Like ideally where should this variable setting be done if done properly in Ember ?

1
Can you duplicate your issue here? emberjs.jsbin.com/nazubi/1/edit?html,js,console,outputKalman
model hooks aren't called when you use transitionTo/transitionToRoute and the model is provided. setupController is only called if the route isn't already on the page, or if the model changes for that route (or a parent route).Kingpin2k
@Kalman Hi I do belive that i stated the problem incorrectly. So I do believe if none of the dynamic segements of a route is changed self transitioning does not occur. So in that case transitionToRoute doesn't bascially call model hook. However changing one of the dynamic segments fires the transitionToRoute causing to go through the model andcontroller hook. I might be wrong but as it appears to me that is the default ember routing behaviour.sajid sharlemin

1 Answers

1
votes

Sorry I'm late and this might not be of any use for you. I just wanted to post it over here, if in case it might be of any use for others.

This link helped me, clear my problem.

Approach 1: We could supply a model for the route. The model will be serialized into the URL using the serialize hook of the route:

var model = self.store.find( 'campaign', { fb_id: fb_id } );
self.transitionToRoute( 'campaign', model);

This will work fine for routing, but the URL might be tampered. For this case, we need to add extra logic to serialize the object passed into the new route and to correct the URL.

Approach 2: If a literal is passed (such as a number or a string), it will be treated as an identifier instead. In this case, the model hook of the route will be triggered:

self.transitionToRoute( 'campaign', fb_id);

This would invoke the model() and would correctly display the required URL on routing. setupController() will be invoked immediately after the model().

2nd one worked fine for me fine. Hope it's useful and answered the above question.