Ember Update Model After Action

I have an ember template that loads some JSON data on page load, there are some buttons and when these buttons are clicked I need to make different json calls and update the model.

Everything works fine in this code, but the model is not updated after starting the action and calling json.

How can I fix this?

App.DatRoute = Ember.Route.extend({ 
    model: function(parms){
        return Em.RSVP.hash({
            datam : Ember.$.getJSON('URL')
        });    
    },
    afterModel: function(){
      $(document).attr('title', 'Title');  
    },
    renderTemplate: function() {
      this.render();
      this.render('fter', { into: 'outlet', outlet: 'fter' });
  },
  actions :{
    action: function(){
        return Em.RSVP.hash({
           datam : Ember.$.getJSON('URL', {data}) 
        });
    }
}
});

      

thank

+3


source to share


1 answer


Because you are not doing anything by updating the model. Amber does nothing with the return value of an action, be it a promise or otherwise. You need to put the action on the controller and set the model with the data returned from the ajax call:

action: function() {
    var self = this;
    Ember.$.getJSON('URL', {data})
        .then(function(result) {
            self.set('model', result);
        });
}

      



or my style, completely equivalent

action: function() {
    var set = this.set.bind(this, 'model');
    Ember.$.getJSON('URL', {data}).then(set);
}

      

+4


source







All Articles