1

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
}
JAKITOVZ
  • 41
  • 2

1 Answers1

0

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": []
}
Leigh F
  • 146
  • 6
  • I updated the JSON response, I accidentally put the `relationships` out of data. btw, I tried creating a model named `promotions-verify` and `pushPayload` but ember is throwing me an error that it's expecting an id from `promotions-verify`. but since `promotions-verify` is not an actual model (just a resource type). I don't have any id to include on my response. But I will look into using Lodash. – JAKITOVZ May 18 '18 at 00:47
  • Ah, I understand. In that case, mapping the data would still work. Otherwise, you could create a serializer for `promotions-verify` and using `normalizeResponse`, manually add in an `id` for each `promotions-verify` object, à la https://discuss.emberjs.com/t/ember-data-records-without-ids-auto-numbering/4824 (old post, but the idea could still work) – Leigh F May 18 '18 at 20:17