samedi 16 janvier 2016

Ember-Cli: prepend findAll queries with a prefix

On one of my routes I need to findAll of my Items, but instead of making the standard /items request, I need to make it to /my/items.

My current solution involves:

export default Ember.Route.extend({
    model() {
        this.store.unloadAll('item');
        return Ember.$.getJSON('/my/items').then((payload) => {
          this.store.pushPayload(payload);
          return this.store.peekAll('item');
        });
    }
});

But unfortunatelly it's not ideal since it requires me to unloadAll() items before making the request as to ensure that the model only returns records just fetched and that any cached records are unloaded.

A solution will probably involve creating a custom adapter specifically for this route and overwriting either the findAll() method or urlForFindAll(), but I'm not sure how to properly create and import a custom adapter.

Just for testing I tried overwriting the default Item adapter, and everything worked, all findAll requests were prepended with /my/:

findAll: function(store, type, sinceToken, snapshotRecordArray) {
  var query, url;
  if (sinceToken) { query = { since: sinceToken }; }

  url = `my${this.buildURL(type.modelName, null, null, 'findAll')}`;

  return this.ajax(url, 'GET', { data: query });
},

..but that obviously overwrites all findAll() queries for this model, wherein I need to make a custom query only in this route.




Aucun commentaire:

Enregistrer un commentaire