15

I'm consuming a web service that in POST/PUT verbs expects a JSON like this:

{
    "id":"CACTU",
    "companyName": "Cactus Comidas para llevar",
    "contactName": "Patricio Simpson",
    "contactTitle": "Sales Agent",
    "address": "Cerrito 333",
    "city": "Buenos Aires",
    "postalCode": "1010",
    "country": "Argentina",
    "phone": "(1) 135-5555",
    "fax": "(1) 135-4892"
}

But Ember Data sends a JSON like this:

{
    "customer": 
    {
        "id":"CACTU",
        "companyName": "Cactus Comidas para llevar",
        "contactName": "Patricio Simpson",
        "contactTitle": "Sales Agent",
        "address": "Cerrito 333",
        "city": "Buenos Aires",
        "postalCode": "1010",
        "country": "Argentina",
        "phone": "(1) 135-5555",
        "fax": "(1) 135-4892"
    }
}

How I can delete "customer" root element when sending POST/PUT operations?

jgillich
  • 71,459
  • 6
  • 57
  • 85
Merrin
  • 514
  • 7
  • 20

1 Answers1

17

You'll want to override one of the serialize methods, I think serializeIntoHash might work:

App.CustomerSerializer = DS.RESTSerializer.extend({
  serializeIntoHash: function(hash, type, record, options) {
    Ember.merge(hash, this.serialize(record, options));
  }
});

This is instead of the normal serializeIntoHash which looks like this:

  serializeIntoHash: function(hash, type, record, options) {
    hash[type.typeKey] = this.serialize(record, options);
  }

Additional documentation can be found here:

https://github.com/emberjs/data/blob/v2.1.0/packages/ember-data/lib/serializers/rest-serializer.js#L595

Eric D. Johnson
  • 10,219
  • 9
  • 39
  • 46
Kingpin2k
  • 47,277
  • 10
  • 78
  • 96
  • it's so easy that I'm ashamed. Thank you very much – Merrin Oct 24 '13 at 18:39
  • Ummm... not so much for me... I did the exact same thing in my serializer, but the result of calling _super() is an object that is already flattened, without a root node. I'm afraid this technique does not work. – Steve H. Oct 24 '13 at 20:04
  • I didn't actually test it out Steve, but it should have been around there, it might actually be the serializeIntoHash method instead of the serialize method, see above. – Kingpin2k Oct 24 '13 at 23:03
  • Thanks for responding, @kingpin2k ... I added 'serializeIntoHash' exactly as you have given along with a log to the console, and I found that serializeIntoHash was not being called. I don't understand how this is working for Merrin. – Steve H. Oct 25 '13 at 21:56
  • @SteveH. would you create a jsbin, or explain in more detail what exactly you're doing? – Kingpin2k Oct 27 '13 at 06:24
  • @kingpin2k Got it- serializeIntoHash as you have given is doing the trick- I had other problems but now working... Thanks! – Steve H. Oct 28 '13 at 21:56