Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete JSON root element for POST/PUT operations in Ember Data

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?

like image 822
Merrin Avatar asked Oct 24 '13 16:10

Merrin


1 Answers

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

like image 161
Kingpin2k Avatar answered Sep 29 '22 03:09

Kingpin2k