(Ember.js) How to save Sideloaded data from ajax call?

128 views Asked by At

Good day!

Just want to ask on how to create a JSONAPISerializer for an ajax call? From what I understand on the docs. I must first create a model before I can make a JSONAPISerializer. But I need to call a custom endpoint that is not listed as my model.

My goal is to pushPayload all the sideloaded data coming from my endpoint. but the problem is :

{  
 "data":[  
  {  
     "type":"promotions-verify",   <----- it's not an actual model 
     "attributes":{  
        "cart-items-discount-in-cents":21900
      },
    "relationships":{...},    <---- sideloaded data that I want to push on ember-data
  }],

 "included": []              <---- sideloaded data that I want to push on ember-data
}
1

There are 1 answers

2
Leigh F On

Is there a reason you can't make a model for promotions-verify? That would be the cleanest way to implement loading in the side-loaded data, since Ember would handle much of the serialization/pushing to the store for you.

If that isn't possible and you make an ajax request, you may need to map the relationships and included payloads to match up with one another (Lodash _.map() could work well for this). Then you can manually push that data (pushPayload) to the Ember store, while ensuring that the items you're pushing also have models and serializers.

Also, I'm not sure if this was accidental, but your example payload doesn't conform to JSON API standards – the relationships object should be nested within data. This will affect how Ember serializes the data, as it's expecting:

{
    "data": [{
        "id": 1,
        "type": "promotions-verify",
        "attributes": {},
        "relationships": {}
    }],
    "included": []
}