vendredi 24 février 2017

Recognize the kind of retrieve-record-object in Ember

I am building a crud-route-mixin where I define default functions and actions for routes.

One of the functions has as argument a query object; within the action I perform the call:

_doSomething(query) {
    query.then( result => {
        //do something default with this result
    })
}

The routes call the _doSomething function with different kind of methods. For example:

Route A

export default Ember.Route.extend(CrudRoute, {
    setupController() {
        this._super(...arguments);
        this._doSomething(this.get('store').findAll('paper'));
    }      
}

Route B

export default Ember.Route.extend(CrudRoute, {
    setupController() {
        this._super(...arguments);
        this._doSomething(this.get('store').findRecord('blog-post'));
    }      
}

I was wondering, is it possible to retrieve the method name or type of the query object? So I could do something like this (pseudo code):

_doSomething(query) {
    query.then( result => {
        if (query.getRetrieveMethodName() === 'findAll') {
            //do something default with this array result
        } else if (query.getRetrieveMethodName() === 'findRecord') {
            //do something default with this single record result
        }
    })
}

P.S. Check if the payload is a single record or array is not an option, because I need this distiction in the error handling as well.




Aucun commentaire:

Enregistrer un commentaire