Ember: How to get computed properties from a nested model?

293 views Asked by At

First: I have no idea how to work with promises in Ember.js. I want to call a property of my controller which depends on async model-data which is also nested.

Also, my model looks something like that:

+-------------+         +------------+ 
| Method      | hasMany |  Practice  | 
|             +--------->            | 
|             |         |            | 
+-------------+         +------------+ 
                              |        
                              | hasMany
                        +-----v------+ 
                        | Alpha      | 
                        |            | 
                        |            | 
                        +------------+

So I created something like this:

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

There are two Problems (like already metioned in the comments):

  1. How to load nested hasMany async models like all alphas in all practices.
  2. How to return the complete result as a property in the RSVP.hash-Method for use in templates or something

Can anybody help me?

Edit 06/20/2015

As @Kingpin2k suggested, Ive added a Gist for better understanding of my Problem: https://gist.github.com/MarcManhart/e5c1d91e8fdfd876de37

1

There are 1 answers

5
Kingpin2k On BEST ANSWER

just return an array, and populate the array after the fact.

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

Update

It looks like pushObjects doesn't like the ED Collection (or maybe it doesn't like the promises underneath, I didn't look into it that much). Also we should use the resolved values instead of the promises sent in (alphas vs resolvedAlphas in my code below).

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