3
votes

Is it possible to set custom url for a specific type?

For example, this is my adapter definition:

/adapters/application.js

import DS from 'ember-data';

export default DS.JSONAPIAdapter.extend({
    namespace: 'v1',
    defaultSerializer: 'JSONSerializer',
    host: 'http://api.example.com'
});

No what I want is to set a custom url for a specific adapter method. By default, each request will be sent to http://api.example.com/v1/{model} but for the store.query() method for example, I would like to tell ember to request http://api.example.com/v1/{model}/search

Thank you

1

1 Answers

2
votes

Yes, you have pathForType for the JSONAPI adapter

Edit:

This is how it works by default:

pathForType: function(modelName) {
    var dasherized = Ember.String.dasherize(modelName);
    return Ember.String.pluralize(dasherized);
  },

You receive the name of the model and you can return a different url.

But, since you want to specify a different url depending on a method, you should use buildURL:

buildURL: function(modelName, id, snapshot, requestType, query) {
    switch (requestType) {
      case 'findRecord':
        return this.urlForFindRecord(id, modelName, snapshot);
      case 'findAll':
        return this.urlForFindAll(modelName);
      case 'query':
        return this.urlForQuery(query, modelName);
      case 'queryRecord':
        return this.urlForQueryRecord(query, modelName);
      case 'findMany':
        return this.urlForFindMany(id, modelName, snapshot);
      case 'findHasMany':
        return this.urlForFindHasMany(id, modelName);
      case 'findBelongsTo':
        return this.urlForFindBelongsTo(id, modelName);
      case 'createRecord':
        return this.urlForCreateRecord(modelName, snapshot);
      case 'updateRecord':
        return this.urlForUpdateRecord(id, modelName, snapshot);
      case 'deleteRecord':
        return this.urlForDeleteRecord(id, modelName, snapshot);
      default:
        return this._buildURL(modelName, id);
    }
  },