1

I'm using Ember API with a JSON API backend. The API accepts filters like this:

/users?filter[simple][name]=John

Right now, whenever I want to make a query, I'm doing this:

this.store.query('users', {
  filter: {
    simple: {
      name: 'John'
    }
  }
});

It would be nice to avoid this verbosity by adding a helper function that works like this:

this.store.simpleQuery('users', { name: 'John' });

That function would pass its arguments directly to query(), just wrapping the query in { filter: { simple: ... } }.

Is this possible? How do I do this?

2 Answers 2

1

Well what is stopping you from creating your own method in the adapter to do exactly that?

// ... your adapter code 

simpleQuery: function(modelName, query) {
    return this.store.query('users', {
        filter: {
            simple: {
                name: 'John'
            }
        }
    });
}

// ...
Sign up to request clarification or add additional context in comments.

Comments

0

You need to extend the default store. Add the following to app/services/store.js

import DS from 'ember-data';

export default DS.Store.extend({
  simpleQuery(modelName, query) {
    return this.query(modelName, {
      filter: { simple: query }
    });
  },

  simpleQueryRecord(modelName, query) {
    return this.queryRecord(modelName, {
      filter: { simple: query }
    });
  }
});

And you'll be able to do this:

this.store.simpleQuery('users', { name: 'John' });
this.store.simpleQueryRecord('users', { email: '[email protected]' });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.