Wednesday, 18 September 2013

When saving a model, data in $.ajaxSetup is not merged

When saving a model, data in $.ajaxSetup is not merged

I have an Ember.js app backed by a RESTful API. Session control is done
through an authentication token: once a user logs in, he appends his
authentication token to each request he makes to the server. I do this by
adding the authentication to the data in $.ajaxSetup.
$.ajaxSetup({
data: { auth_token: this.get('authToken') }
});
Now, this works fine for GET requests. However, when saving models to the
server through a POST or PUT request, the Ember Data RESTAdapter
stringifies the data object. In DS.RESTAdapter.ajax it does
....
if (hash.data && type !== 'GET') {
hash.contentType = 'application/json; charset=utf-8';
hash.data = JSON.stringify(hash.data);
}
...
Because of this, the authentication token is not merged into the data. In
this jQuery ticket they say that it's something they are never going to
support.
What's the most elegant way of solving this? I'd rather not override the
Ember's RESTAdapter.ajax function because the code is changing so quickly
so my overridden function might not be compatible with the rest of the
codebase at the next release.

No comments:

Post a Comment