1

首先:我不知道如何在 Ember.js 中使用 Promise。我想调用我的控制器的一个属性,该属性取决于也是嵌套的异步模型数据。

此外,我的模型看起来像这样:

+-------------+         +------------+ 
| 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()

有两个问题(就像评论中已经提到的那样):

  1. 如何加载嵌套的 hasMany 异步模型,如所有实践中的所有 alpha。
  2. 如何将完整结果作为 RSVP.hash-Method 中的属性返回以在模板或其他内容中使用

有谁能够帮助我?

编辑 06/20/2015

正如@Kingpin2k 建议的那样,我添加了一个要点以更好地理解我的问题: https ://gist.github.com/MarcManhart/e5c1d91e8fdfd876de37

4

1 回答 1

2

只需返回一个数组,并在事后填充数组。

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 收藏(或者它不喜欢下面的承诺,我没有深入研究它)。此外,我们应该使用解析的值而不是发送的承诺(alphasresolvedAlphas我下面的代码中)。

示例:http ://emberjs.jsbin.com/cinobetoyu/1/edit?js,output

于 2015-06-19T20:12:14.943 回答