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

2つの問題があります(コメントですでに言及されているように):

  1. すべてのプラクティスですべてのアルファのように、ネストされた hasMany 非同期モデルをロードする方法。
  2. テンプレートなどで使用する RSVP.hash-Method のプロパティとして完全な結果を返す方法

誰でも私を助けることができますか?

2015 年 6 月 20 日編集

@Kingpin2k が示唆したように、問題をよりよく理解するために Gist を追加しました: 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()

アップデート

pushObjectsEDコレクションが気に入らないようです(または、下の約束が気に入らないのかもしれません。あまり調べていませんでした)。また、送信されたプロミスの代わりに解決された値を使用する必要があります(以下のコードではalphasvs )。resolvedAlphas

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

于 2015-06-19T20:12:14.943 に答える