[英]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. 我有一个ember模板,在页面加载时加载一些JSON数据,有一些按钮,当点击这些按钮时,我需要进行不同的json调用并更新模型。
Everything works fine in this code but the model is not being updated after the action is triggered and the json call is made. 在此代码中一切正常,但在触发操作并进行json调用后,模型不会更新。
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 谢谢
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. Ember对动作的返回值没有任何作用,无论是承诺还是其他。 You need to put the action on the controller and set the model with the data coming back from the ajax call: 您需要将操作放在控制器上,并使用从ajax调用返回的数据设置模型:
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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.