How to save Sideloaded data from ajax call?


#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
}

#2

In your example the relationships entry is in the wrong place, and also relationships does not directly relate to sideloading.

The "included" key shohuld contain the resources to sideload, whereas relationships specifies the belongsTo and hasMany relationships for an individual resource. The relationships key can only be specified inside a resource definition.

In your example where you’re performing a manual AJAX call that returns a JSON API payload, you can push it directly into the Ember Data store using pushPayload(). Doing this will cause Ember Data to update all its internal models in the store based on any sideloaded content you provide.


#3

@richard-nz my bad, I accidentally encoded a wrong JSON response. but the correct one is having the relationships included inside the data. btw I tried using pushPayload but as I mention, promotions-verify is not an existing model and that is where Im having a problem.


#4

The types of the top-level resource and any sideloaded resources passed in in to pushPayload() must correspond to known Ember Data models. ED has no idea what to do with random additional types you try to push in, it doesn’t know anything about their schema etc… where are you even expecting it to store this information … ?

This may be state that doesn’t belong in the relational ED store at all - i.e. it may be state that you need to store on a service, on a component, or in local storage. You can pull it off the manual AJAX request yourself and store it wherever it fits best.