vendredi 5 août 2016

Ember : fetch data afterwards

I get some data from my API through model in route.js. This data contains somewhere an id on its own, with no relationships or included stuff to get details. So I have to make another API request to get the object with that id.

I did it with a component (to be able to send the id argument) and it works, but I would like to know if that's the way to go and if so, if I did it right and it cannot be simplified (because it looks complex to me for such a simple task):

I call my component with

Component template just contains

component.js:

import Ember from 'ember';
export default Ember.Component.extend({
    store: Ember.inject.service(),
    _getResource(id) {
        return this.get('store').findRecord('resource', id);
    },
    resource: Ember.computed('id', function() {
        const id = this.get('id');
        const proxy = Ember.ObjectProxy.extend(Ember.PromiseProxyMixin);
        return proxy.create({
            promise: this._getResource(id)
        });
    }),
    name: Ember.computed('resource.isFulfilled', function() {
        if (this.get('resource.isFulfilled')) {
            return `${this.get('resource.lastName')} ${this.get('resource.firstName')}`;
        }
        else {
            return "...";
        }
    }),
    didReceiveAttrs() {
        const id = this.getAttr('id');
        Ember.assert('resource-name must have an "id" attribute!', !Ember.isBlank(id));
    }
});




Aucun commentaire:

Enregistrer un commentaire