PUT Requests with Custom Ember-Data REST Adapter
I am using Ember-Data 1.0.0.Beta-9 and Ember 1.7 to consume REST APIs through DreamFactory REST framework. ( http://www.dreamfactory.com ).
I had to extend the RESTAdapter to use DF and I was able to make GET and POST requests without any problems. Now I am trying to execute requests model.save()
(PUT) and I am having severe hiccups.
The call to model.save () sends a PUT request with correct data to the API endpoint and I get a 200 OK response with a JSON response { "id": "1" }
that should happen. However, when I try to access the updated record, all properties are empty except for the ID and the record on the server is not updated. I can take the same JSON string passed in the request, insert it into the Swagger Dreamfactory API Docs and it doesn't work - the answer is good and the record is updated in the DB.
I created a JSBin to show all the code http://emberjs.jsbin.com/nagoga/1/edit
Unfortunately, I cannot have a live example as the servers in question are blocked to only accept applications from our company's public IP range.
DreamFactory provides a live demo of the API in question https://dsp-sandman1.cloud.dreamfactory.com/swagger/#!/db/replaceRecordsByIds
source to share
OK, in the end I found that you can customize DreamFactory's response by adding a parameter ?fields=*
to the end of the PUT request. I neutralized this my method updateRecord
using the following:
updateRecord: function(store, type, record) {
var data = {};
var serializer = store.serializerFor(type.typeKey);
serializer.serializeIntoHash(data, type, record);
var adapter = this;
return new Ember.RSVP.Promise(function(resolve, reject) {
// hack to make DSP send back the full object
adapter.ajax(adapter.buildURL(type.typeKey) + '?fields=*', "PUT", { data: data }).then(function(json){
// if the request is a success we'll return the same data we passed in
resolve(json);
}, function(reason){
reject(reason.responseJSON);
});
});
}
And poof we are interested in updates!
DreamFactory supports support for multiple parameters at the end of requests to completely customize the response - at some point I'll try to implement this correctly, but for now I can move forward with my project. Hooray!
source to share