mercredi 5 août 2015

How to create computed attribute in controller based on store information and model hasMany relationship

I have a model for a type of object "Module". Simplified, it is like this:

App.Module = DS.Model.extend({
    name: DS.attr('string'),
    orModules: DS.hasMany('module', { inverse: null }),  // reflexive relship
});

In the view of the Module, there should be a list of all Module records that are not the current model, and not contained in model.orModules. I think this information does not belong in the App.Module but in the App.ModuleController.

The problem is that I still haven't got my head around the promises and asynchronous stuff, and the "classes" in Ember and DS like DS.PromiseArray.

Good, this is my attempt:

App.ModuleController = Ember.Controller.extend({

    availableModules: function () {
        var thisModule = this.get('model');  
        var allModules = this.store.findAll('module');  // this is a promise, right?
        var orModules = thisModule.get('orModules');  // this as well, more exactly a `DS.PromiseArray`, right?

        return DS.PromiseArray.create({
            promise: Ember.RSVP.hash({allMods: allModules, orMods: orModules}, function (res) {
                // Here I guess both promises are fulfilled, so I 
                // have access to all data

                // But, first problem res.allMods and res.orMods have different interfaces. 
                // I can access res.allMods.content for the Module objects, and call mod.get('id'), but I have trouble transversing res.orMods.

                // And the second and big problem is, I don't know what should I return.
                // even returning some hardcoded value raises an exception 
                // "Assertion Failed: ArrayProxy expects an Array or Ember.ArrayProxy, but you passed object
                return [{id: 666, name: '666'}];
            })

        });

    }.property('model.orModules'),

});

So, as you can see I'm pretty confused. Any help? thanks in advance.




Aucun commentaire:

Enregistrer un commentaire