【发布时间】:2017-04-17 02:19:52
【问题描述】:
我有一个带有简单属性的 Ember 控制器,它应该从服务器获取文件列表。服务器端一切正常,数据正在返回并显示在控制台上。
在控制器中我有:
imgFiles: new Ember.RSVP.Promise(function(resolve, reject) {
Ember.$.ajax(ENV.apiHost+'/'+ENV.apiNamespace +'/folderwalk/newsimg', {
success: function(response) {
// console.log(response);
resolve(response);
},
error: function(reason) {
reject(reason);
}
});
}),
在模板中:
{{imgFiles}} // shows [object Object]
{{#each imgFiles as |file|}} // doesn't loop at all
x {{file}}
{{/each}}
我已经尝试了所有我能想到的变化。封装在函数中,返回 Promise,成功返回响应,...
我仍然无法得到返回实际数据的承诺。
【问题讨论】:
-
Still I can not get the promise to return the actual data- 承诺是对未来价值的承诺(或类似的东西) - 在您的代码中没有任何地方使用您需要的唯一功能....then- 是一个Promise 必须具有的方法,也是获得“价值”的唯一方法 - 提示:imgFiles.then(result => console.log(result))- 将记录在resolve(response)解决的问题 -
好的,谢谢!这会将其记录到控制台中。但是我怎么能退货呢?我试过
.then(result => return result)这当然给了我一个错误.... -
恐怕我不知道如何在 ember 中正确使用 Promise - 我只能告诉你 Promise 到底是什么
标签: javascript ember.js rsvp-promise