jsonnode.jsserializationember.jsember-model

How can I serialize a Simple array payload in ember?


I am pretty new to Ember. I have a service which returns a simple array like

[
  "abc",
  "bcd",
  "cde",
  "def",
  "efg"
]

My model is somewhat like this

import Model from 'ember-data/model';
import attr from 'ember-data/attr';

export default Model.extend({
  value: attr()
});

In the serializer(I am trying with RESTSerializer), I want this data to be sent back to route.js where the service call is made. The service call is to an API which I am not allowed to change in any way.

I tried a lot of ways that might be stupid and googled a lot. Sadly I could not find a solution though I believe it may not be too tough.

I got the payload in serializer as pasted above and was able to log the response. From there what is to be returned and what serializer is apt is my current problem. Kindly ask me if any further details are required to figure this out. I am not posting much so that I can keep it simple and understandable. Any help is appreciated.


Solution

  • You may not want to use Ember Data. However, you can by implementing normalizeResponse in your Serializer.

    For example, if your model name is "account":

    export default DS.RESTSerializer.extend({
        normalizeResponse(store, primaryModelClass, payload, id, requestType) {
            let newPayload= {
                accounts: [{
                    value: payload
                }]
            };
            return this._super(store, primaryModelClass, newPayload, id, requestType);
        }
    });