Remove request payload's root node in JSON not working


#1

In a recent project, we use the latest ember-data v2.1, because our API service is not fully REST one, so we had to make some customizations.

One of them is to get rid of the root node in the request’s payload JSON, which ember-data automatically populates like this:

{"session":{"username":"example","password":"example"}}

but our server expects like this:

{"username":"example","password":"example"}

So, I read the API doc and find this: DS.RESTSerializer#serializeIntoHash, and did a little overwrite:

export default DS.RESTSerializer.extend({
  serializeIntoHash(hash, typeClass, snapshot, options) {
    Ember.merge(hash, this.serialize(snapshot, options))
  }
})

but this will not work as we expected, it generates a empty payload for the ajax request. I try to invoke the parent then:

export default DS.RESTSerializer.extend({
  serializeIntoHash(hash, typeClass, snapshot, options) {
    hash = this.serialize(snapshot, options)
    this._super(...arguments)
  }
})

this did works, but instead it returns a payload like below:

{
  "username":"example",
  "password":"example",
  "session":{"username":"example","password":"example"}
}

this is so annoying! how to clean it up in a correct way?


#2

hmm, look like you need to use JsonAdapter if the format like that. isn’t it?

but if want to use REST Adapter, it look like you should override the normalize method.


#3

Yeah, I’ve realized this, RESTAdapter is not suit for our situation, thanks for your suggestion.