jeudi 1 septembre 2016

Ember.js: promises in a computed property

First: I know it is not the desired way to have promises in a computed property. However in my setup it fits best. I currently use Ember 1.8.0-beta.1 for compatibility purposes.

What I am trying to accomplish is:

  • get all weeknummers including the desired color
  • make links of them

In my controller:

weeknumbersChoicesWithColor: function () {
    var weeknumberChoices = this.get('weeknumbersChoices');
    var yearnumber = this.get('yearnumber');
    var self = this;

    var promises = [];
    weeknumberChoices.forEach(function (weeknumber) {
        var promise = self.store.findQuery('order', {'weeknumber': weeknumber, 'yearnumber': yearnumber}).then(function(orders){
            return orders.set('weeknumber', weeknumber);
        });
        promises.push(promise);
    });

    return Ember.RSVP.all(promises).then(function(result){

        result.forEach(function(weekOrders){
            // ... do something to get weeknumer and color

            return {
                weeknumber: weeknumber,
                color: color
            };
        });
    });
}.property('yearnumber', 'weeknumber'),

In my template:


     <li></li>


My template however trows this error: Uncaught Error: Assertion Failed: The value that #each loops over must be an Array. You passed {_id: 131, _label: undefined, _state: undefined, _result: undefined, _subscribers: } This is of course because the promise is not fullfilled yet. When I wrap them in a block it also does not work too.

It does work when I do not return a promise, but set a property in the controller with the array, with an Ember.run.later however that seems to much hacking to me and will not always work.




Aucun commentaire:

Enregistrer un commentaire