1

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

EchtFettigerKeks
  • 1,692
  • 1
  • 18
  • 33

1 Answers1

2

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

Kingpin2k
  • 47,277
  • 10
  • 78
  • 96
  • Thanks for the answer @Kingpin2k, but it seems your approach missing requesting all the concrete 'alpha'-recods. In your lines: `alphaFields.forEach(function(afs){ returnValue.pushObjects(afs);` _afs_ is **JUST** the field in a practice-record with the IDs of the related alphas. For example the (practice ID 1) has two related _alpha_-IDs like this: `alphas: [4,7];` So I have also request via RSVP (or something) the concrete alpha-records out of each _afs_. Can you also show me that? – EchtFettigerKeks Jun 19 '15 at 23:59
  • Can you show the models, it'll make it easier for me to conceptualize what you're saying. – Kingpin2k Jun 20 '15 at 05:52
  • sure! thank you. In this gist all necessary files are included (the model-files are _alpha.js_, _method.js_ and _practice.js_ Would be great if you can help me! – EchtFettigerKeks Jun 20 '15 at 10:21
  • Can you verify that `practices.getEach('alphas')` is returning an array of Promises? –  Jun 20 '15 at 22:40
  • 1
    @Kingpin2k i'm in tears: You are my hero today! That (your snippet) works like a charm! Thank you so much! – EchtFettigerKeks Jun 21 '15 at 09:25