Search code examples
ember.jsember-model

Configuring RESTAdapter to not set the .json extension for get / list requests


I'm using a cross domain REST api. I have defined my custom REST adapter to trigg my API. Pb is to remove the ".json" automaticaly set by ember-model.

How to configure my adapter to avoid setting my "replace function" (url=url.replace('.json', '');)

App.Book.adapter = Ember.RESTAdapter.create({
  ajaxSettings: function(url, method) {
    var authorization= "Basic " + btoa("login" + ":" + "pwd");
    url=url.replace('.json', '');
    return {
      url: url,
      type: method,
      dataType: "json",
      headers: {
        "Authorization": authorization
      },
    };
  }
});
App.Certificate.url='http://mysite/api/v1/books';

Solution

  • How to configure my adapter to avoid setting my "replace function" (url=url.replace('.json', '');)

    Since ember-model does not provide any configuration option to change this behaviour, IMHO, your solution by doing url = url.replace('.json', ''); isn't that bad.

    Another possible way I can think of could be to reopen the RESTAdapter and override the buildURL function to not include the .json.

    Ember.RESTAdapter.reopen({
      buildURL: function(klass, id) {
        var urlRoot = Ember.get(klass, 'url');
        if (!urlRoot) { throw new Error('Ember.RESTAdapter requires a `url` property to be specified'); }
    
        if (!Ember.isEmpty(id)) {
          return urlRoot + "/" + id;
        } else {
          return urlRoot;
        }
      }
    });
    

    But this is not that future proof if the original code changes and you want to update the lib you had to change also your override.

    Hope it helps.