3
votes

I'm using Ember-Data 1.0.0.Beta-9 and Ember 1.7 to consume a REST API via DreamFactory's REST Platform. (http://www.dreamfactory.com).

I've had to extend the RESTAdapter in order to use DF and I've been able to implement GET and POST requests with no problems. I am now trying to implement model.save() (PUT) requests and am having a serious hiccup.

Calling model.save() sends the PUT request with the correct data to my API endpoint and I get a 200 OK response with a JSON response of { "id": "1" } which is what is supposed to happen. However when I try to access the updated record all of the properties are empty except for ID and the record on the server is not updated. I can take the same JSON string passed in the request, paste it into the DreamFactory Swagger API Docs and it works no problem - response is good and the record is updated on the DB.

I've created a JSBin to show all of the code at http://emberjs.jsbin.com/nagoga/1/edit

Unfortunately I can't have a live example as the servers in question are locked down to only accept requests from our company's public IP range.

DreamFactory provides a live demo of the API in question at https://dsp-sandman1.cloud.dreamfactory.com/swagger/#!/db/replaceRecordsByIds

2
Are you returning the correct JSON back to Ember? Ember Data expects the full object back, formatted in exactly the same way that you would get if you called model.serialize(), except that the id property should be populated for the model, plus any nested/embedded related models.Josh Padnick
I'm trying to pull the model from the store with store.find() but the result is empty. You can see it in the saveOrder method on the route in the jsBin. Not sure if that's correct or not.ultimatemonty
Yeah, if I'm correct, all the info I would need is in your post itself and the JSBin doesn't matter. What I'm saying is that I believe the RESTSerializer expects a response richer than just { id: 1 }. Check out stackoverflow.com/questions/14922623/… and see if that helps. I'm not 100% sure this is the issue, though.Josh Padnick
Unfortunately I don't have any control over the response. While looking at EmberParseAdapter I saw where @mixonic had to override some serialization methods because Parse only sent back the objectId and a couple of timestamps. Possible I need to do the same?ultimatemonty

2 Answers

3
votes

OK in the end I discovered that you can customize the DreamFactory response by adding a ?fields=* param to the end of the PUT request. I monkey-patched that into my updateRecord method 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 haz updates!

DreamFactory has support for tacking several params onto the end of the requests to fully customize the response - at some point I will look to implement this correctly but for the time being I can move forward with my project. Yay!

0
votes

EmberData is interpreting the response from the server as an empty object with an id of "1" an no other properties in it. You need to return the entire new object back from the server with the changes reflected.