javascript-将Ember.js控制器变量设置为Ember Data对象,而不是Promise
作者:互联网
我的路线最初没有任何建议.基于一个动作,我想使用Ember Data来获取一个建议数组,获取第一个建议并将其分配给控制器.这是我得到的:
App.IndexRoute = Ember.Route.extend({
setupController: function(controller, model) {
this._super(controller, model);
controller.set('suggestion', null);
},
actions: {
getSuggestion: function() {
suggestion = this.store.find('suggestion').then(function(s) {
return s.get('firstObject');
});
this.controller.set('suggestion', suggestion);
}
}
});
问题在于,建议变量在执行getSuggestion操作之后仍然是一个承诺.解决承诺后,如何仅设置控制器变量?或者如何让它稍后解析并使用实际对象更新变量?
解决方法:
您应该在“ then”块内设置“建议”
App.IndexRoute = Ember.Route.extend({
setupController: function(controller, model) {
this._super(controller, model);
controller.set('suggestion', null);
},
actions: {
getSuggestion: function() {
controller = this.controller;
this.store.find('suggestion').then(function(s) {
suggestion = s.get('firstObject');
controller.set('suggestion', suggestion);
});
}
}
});
标签:promise,ember-js,ember-data,javascript 来源: https://codeday.me/bug/20191028/1951675.html