最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Ember Update model after action - Stack Overflow

programmeradmin1浏览0评论

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 being updated after the action is triggered and the json call is made.

How can I fix it?

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}) 
        });
    }
}
});

Thanks

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 being updated after the action is triggered and the json call is made.

How can I fix it?

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}) 
        });
    }
}
});

Thanks

Share Improve this question asked Nov 9, 2014 at 4:22 rkshrksh 4,05010 gold badges52 silver badges72 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

Because you're not doing anything that updates the model. Ember does nothing with the return value from an action, be it a promise or otherwise. You need to put the action on the controller and set the model with the data ing back from the ajax call:

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

or my style, entirely equivalent

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

评论列表(0)

  1. 暂无评论