3

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

ultimatemonty
  • 211
  • 1
  • 12
  • 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 Aug 22 '14 at 20:53
  • 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 Aug 22 '14 at 20:55
  • 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 http://stackoverflow.com/questions/14922623/what-is-the-complete-list-of-expected-json-responses-for-ds-restadapter and see if that helps. I'm not 100% sure this is the issue, though. – Josh Padnick Aug 22 '14 at 20:59
  • 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 Aug 22 '14 at 21:04

2 Answers2

3

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!

ultimatemonty
  • 211
  • 1
  • 12
0

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.

Chuck
  • 888
  • 1
  • 9
  • 20
  • yeah I think I'm going to have to override the `updateRecord` method similar to https://github.com/clintjhill/ember-parse-adapter/blob/master/dist/ember-parse-adapter.js#L290 – ultimatemonty Aug 22 '14 at 21:10
  • I was planning on open sourcing the adapter I've been working on so I went ahead and created a Github project for it at https://github.com/ultimatemonty/ember-data-dreamfactory-adapter. I've been working on overriding `updateRecord` but can't seem to get the response right. – ultimatemonty Aug 25 '14 at 12:51