Search code examples
ember.jsember-dataember-cli

Custom API calls with Ember Data


I don't know if this is possible with Ember data, although I am kind of doubtful, but the API I am working with supports being able to produce 'or' queries by hitting URLs like,

/v1/subjects?first_name||last_name=*dr*

this would result in a query that looks like

select *
from subjects
where first_name like '%dr%'
or last_name like '%dr%'

However, is it even possible with Ember data to produce such a query?

this.store.find('subject', {
    ...
});

Given the way that ember data builds the query field names from object properties, it seems unlikely that it would support this kind of custom request.

If that is the case, what would be the best way to produce such a custom request? Just using jquery to write custom ajax requests?


Solution

  • You'll want to override the urlForFindQuery method in your adapter. Looking at the default source here, you can probably come up with something pretty simple:

    export default DS.RESTAdapter.extend({
        urlForFindQuery(query, modelName) {
            const url = this._buildURL(modelName);
            return `${url}?first_name||last_name=*${query.search_term}*`;
        }
    });
    

    Obviously that's not a fully working example, but hopefully you get the idea.