Ember: 如何从嵌套模型中获取计算属性?
Ember: How to get computed properties from a nested model?
首先:我不知道如何使用 Ember.js 中的承诺。
我想调用我的控制器的 属性,它依赖于同样嵌套的异步模型数据。
此外,我的模型看起来像这样:
+-------------+ +------------+
| Method | hasMany | Practice |
| +---------> |
| | | |
+-------------+ +------------+
|
| hasMany
+-----v------+
| Alpha |
| |
| |
+------------+
所以我创建了这样的东西:
allAlphas: function() {
var self = this;
var returnValue = "nichts";
var promises = {
allAlphas: self.get('model.method').then(function(method) {
//get the practices
return method.get('practices');
}).then(function(practices) {
//get the alphaSField in EVERY practice
//the alphasField is the (hasmany 'alpha')member in practice
var alphasFields = practices.getEach('alphas');
return Ember.RSVP.all(alphasFields).then(function() {
return alphasFields;
});
}).then(function(alphasFields) {
// here: get all the alphas via promise or something
})
};
Ember.RSVP.hash(promises).then(function(results) {
// return all the alphas (of all pracitces in the method) in some way
});
}.property()
有两个问题(就像评论中已经提到的):
- 如何加载嵌套的 hasMany 异步模型,就像所有实践中的所有 alpha 一样。
- 如何在 RSVP.hash 中将完整结果 return 作为 属性 - 用于模板或其他东西的方法
有人可以帮我吗?
编辑 2015 年 6 月 20 日
正如@Kingpin2k 所建议的,我添加了一个要点以便更好地理解我的问题:
https://gist.github.com/MarcManhart/e5c1d91e8fdfd876de37
只是 return 一个数组,并在事后填充数组。
allAlphas: function() {
var self = this,
returnValue = [];
this.get('model.method').then(function(method) {
//get the practices
return method.get('practices');
}).then(function(practices) {
//get the alphasField in EVERY practice
//the alphasField is the (hasmany 'alpha')member in practice
var alphas= practices.getEach('alphas');
Ember.RSVP.all(alphas).then(function(resolvedAlphas) {
resolvedAlphas.forEach(function(afs){
returnValue.pushObjects(afs.toArray());
});
});
});
return returnValue;
}.property()
更新
看来pushObjects
不喜欢ED合集(或者不喜欢下面的promises,我没怎么看)。此外,我们应该使用已解决的值而不是发送的承诺(我下面的代码中的alphas
vs resolvedAlphas
)。
首先:我不知道如何使用 Ember.js 中的承诺。 我想调用我的控制器的 属性,它依赖于同样嵌套的异步模型数据。
此外,我的模型看起来像这样:
+-------------+ +------------+
| Method | hasMany | Practice |
| +---------> |
| | | |
+-------------+ +------------+
|
| hasMany
+-----v------+
| Alpha |
| |
| |
+------------+
所以我创建了这样的东西:
allAlphas: function() {
var self = this;
var returnValue = "nichts";
var promises = {
allAlphas: self.get('model.method').then(function(method) {
//get the practices
return method.get('practices');
}).then(function(practices) {
//get the alphaSField in EVERY practice
//the alphasField is the (hasmany 'alpha')member in practice
var alphasFields = practices.getEach('alphas');
return Ember.RSVP.all(alphasFields).then(function() {
return alphasFields;
});
}).then(function(alphasFields) {
// here: get all the alphas via promise or something
})
};
Ember.RSVP.hash(promises).then(function(results) {
// return all the alphas (of all pracitces in the method) in some way
});
}.property()
有两个问题(就像评论中已经提到的):
- 如何加载嵌套的 hasMany 异步模型,就像所有实践中的所有 alpha 一样。
- 如何在 RSVP.hash 中将完整结果 return 作为 属性 - 用于模板或其他东西的方法
有人可以帮我吗?
编辑 2015 年 6 月 20 日
正如@Kingpin2k 所建议的,我添加了一个要点以便更好地理解我的问题: https://gist.github.com/MarcManhart/e5c1d91e8fdfd876de37
只是 return 一个数组,并在事后填充数组。
allAlphas: function() {
var self = this,
returnValue = [];
this.get('model.method').then(function(method) {
//get the practices
return method.get('practices');
}).then(function(practices) {
//get the alphasField in EVERY practice
//the alphasField is the (hasmany 'alpha')member in practice
var alphas= practices.getEach('alphas');
Ember.RSVP.all(alphas).then(function(resolvedAlphas) {
resolvedAlphas.forEach(function(afs){
returnValue.pushObjects(afs.toArray());
});
});
});
return returnValue;
}.property()
更新
看来pushObjects
不喜欢ED合集(或者不喜欢下面的promises,我没怎么看)。此外,我们应该使用已解决的值而不是发送的承诺(我下面的代码中的alphas
vs resolvedAlphas
)。