【发布时间】:2018-02-24 21:48:04
【问题描述】:
我已经尝试了几个版本/尝试来创建 RSVP.Promise 并将其作为参数返回到我的模板。
所有的 console.log 都给出了合理的值,所以承诺 正在解决。 我遇到的问题(这也是问题)是如何将解析值返回到我的模板。
这是我尝试过的版本:
// in controller.js
testA: Ember.computed('sessionAccount.account.id', function() {
let _this = this;
let promise = new Ember.RSVP.Promise(function(resolve, reject) {
_this.get('store').findAll('accounts2workgroup').then(function(a2ws) {
let workgroups = [];
a2ws.forEach(function(a2w){
if(a2w.get('rights')>1) {
workgroups.push(a2w.get('workgroup'));
}
});
console.log(workgroups);
_this.set('wgAsAdmin', workgroups); // this works
resolve(Ember.A(workgroups)); //=> [Object] in rendered template
// return workgroups; // no, not that way
});
});
promise.then(function(data) {
console.log('did resolve');
console.log(data);
})
return promise;
}).property('sessionAccount.account.id'),
testB: Ember.computed('sessionAccount.account.id', function() {
return new Ember.RSVP.Promise(function(resolve, reject) {
let workgroups = Ember.ArrayProxy.create([{'label': 'TestB Label'}]);
resolve(workgroups);
});
}),
testC: Ember.computed(function() {
return this.store.findAll('artists2workgroup').then(function(a2ws) {
let workgroups = [];
a2ws.forEach(function(a2w){
if(a2w.get('rights')>1) {
workgroups.push(a2w.get('workgroup'));
}
});
console.log(workgroups);
return workgroups; //=> [Object] in rendered
});
}),
testD: Ember.computed(function() {
return this.store.findAll('workgroup'); // this of course works, but that's not what I want...
}),
在我的模板中,我像这样测试我的所有测试:
<h4>TestB</h4>
{{#each testB as |wg|}}
{{wg}}<br>
{{wg.label}}<br>
{{/each}}
testB: {{testB}}<br>
testB.length: {{testB.length}}<br>
所有(显然是最后一个 testD 除外)都渲染到
测试B
testB: [对象对象]
测试B.长度:
虽然我希望/希望他们展示
测试B
BB-促销
testB:
testB.length: 1
我知道有一些方法可以解决这个问题(我可以在解析 f.e. 时设置另一个属性),但希望以正确的方式进行操作并学习如何执行此操作。 我知道,这些例子没有太大意义。这只是基本功能,一旦我开始运行它就会得到增强。
【问题讨论】:
标签: javascript ember.js htmlbars