Add params to Ember Data's RESTAdapter AJAX Requests

Ember Data is a persistence layer for Ember.Js. Ember Data is a library that integrates tightly with Ember.js to make it easy to retrieve records from a server, cache them for performance, save updates back to the server, and create new records on the client.

Ember Data makes it really simple to communicate with a RESTful JSON API by automatically creating appropriate urls for REST API calls and serializing/desserialzing parameters as required. However, for certain tasks, it is important to be able to customize certain things that Ember Data automates for you. For example, you might want to add some parameters to all the requests that you send through Ember Data in order to authenticate the user. As with everything else in Ember, you could do it with very little extra code. Just override the ajax method in RESTAdapter to add your params and you are done.

App.RESTAdapter = DS.RESTAdapter.extend({
  // Scope all ajax calls.
  ajax: function(url, type, hash) {
    if (Ember.isEmpty(hash)) hash = {};
    if (Ember.isEmpty(hash.data)) hash.data = {};
    hash.data.access_token = App.accessToken; // Add an access token param.
    return this._super(url, type, hash);
  }
});
Published 26 Jan 2014

I build mobile and web applications. Full Stack, Rails, React, Typescript, Kotlin, Swift
Pulkit Goyal on Twitter