【问题标题】:Ember: How to get computed properties from a nested model?Ember:如何从嵌套模型中获取计算属性?
【发布时间】:2015-06-19 16:10:20
【问题描述】:

首先:我不知道如何在 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()

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

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

谁能帮帮我?

编辑 06/20/2015

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

【问题讨论】:

    标签: ember.js ember-cli rsvp-promise


    【解决方案1】:

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

    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

    【讨论】:

    • 感谢@Kingpin2k 的回答,但您的方法似乎缺少请求所有具体的“alpha”记录。在您的行中:alphaFields.forEach(function(afs){ returnValue.pushObjects(afs); afs JUST 是练习记录中的字段,其中包含相关 alpha 的 ID。例如(实践 ID 1)有两个相关的 alpha-ID,如下所示:alphas: [4,7]; 所以我还通过 RSVP(或其他)请求每个 afs 中的具体 alpha 记录。你也可以给我看看吗?
    • 你能展示模型吗,这样我更容易理解你在说什么。
    • 当然!谢谢你。在这个要点中包括了所有必要的文件(模型文件是 alpha.jsmethod.jspractice.js 如果你可以帮帮我!
    • 你能验证practices.getEach('alphas') 正在返回一个 Promise 数组吗?
    • @Kingpin2k 我流泪了:今天你是我的英雄!那(你的 sn-p)就像一个魅力!非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2016-02-05
    • 2014-01-07
    • 1970-01-01
    • 2014-10-09
    • 2014-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多