mercredi 5 août 2015

Ember.js computed property on controller based on async relationships

I'm new to both Ember.js and promises, and so far I'm having trouble getting my head around this problem.

This is somehow my model for a "Module". Modules can have "alternative Modules", which is modelled as orModules.

App.Module = DS.Model.extend({
    name: DS.attr('string'),
    number: DS.attr('string'),
    classification: DS.attr('string'),

    orModules: DS.hasMany('module', { inverse: null }),
});

In the template, I want to show the properties, the orModules, and all the available modules that are not used in the orModules relationship. Since this availableModules is not something persistent in the backend, I think that a computer property in the controller is the way to go.

But it is my understanding that calls to this.store and model.get(...async relship...) return promises, so I have to wait for both of them to finish, and then compute the available modules.

I have read some docs from RSVP and I tried this:

 App.IndexModuleController = Ember.ObjectController.extend({

    availableModules: function () {
        var allModules = this.store.findAll('module'),  // this should be a promise
            thisModule = this.get('model'),
            orModules = thisModule.get('orModules');  // this as well

        return Ember.RSVP.all([allModules, orModules]).then(function (values) {
            var allModules = values[0],
                orModules = values[1];

            // calculate difference between them
            return difference;
        });
    }.property('model.orModules'),
});

But it does not work, the console logger says:

Uncaught Error: Assertion Failed: The value that #each loops over must be an Array. You passed {_id: 99, _label: undefined, _state: undefined, _result: undefined, _subscribers: }

I guess that means that that RSVP.all does not return the promise Ember is expecting... Any help? thanks in advance.

PS: I have checked these questions, but I could not solve my problem:




Aucun commentaire:

Enregistrer un commentaire