Reputation: 529
I am trying to change the model for a template at runtime when a button is pressed. For changing the model of template called 'example', changeModel action is called on button press. This method makes an Ajax request and gets some data from the backend. Now the problem is that the app transitions to the example template but the new data is not displayed in the template. I verified that the data is being sent from the server. When I put exactly same data into a variable and try to load that as a model, it works. What might be the problem? Is the problem because the Ajax request is not resolved before transitioning to the new page and I need to use promise somehow? Or is the data type of data being sent different? If so, how can I convert it into an array. This question is in a way linked to the other question I asked a few days back: Rendering a template and invoking model hook in Ember.js. You can have a look at the code I am using.
changeModel: function(data) {
var url = "/ChangeModel";
var newModel= Ember.$.getJSON(url).then(function(data) {
return data;
});
var self = this;
this.transitionTo('example').then(function(){
self.controllerFor('example').set('model', newModel);
});
}
Upvotes: 1
Views: 1059
Reputation: 1952
The varible 'newModel' seams to be a promise.
Try this: replace your code:
self.controllerFor('example').set('model', newModel);
with
newModel.then(
function(data) {
self.controllerFor('example').set('model', data);
});
Upvotes: 2