Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning record(s) after store pushPayload call

Is there a better way to return the record(s) after DS.Store#pushPayload is called? This is what I'm doing...

var payload = { id: 1, title: "Example" }
store.pushPayload('post', payload);
return store.getById('post', payload.id);

But, with regular DS.Store#push you get the inserted record returned. The only difference between the two, from what I can tell, is that DS.Store#pushPayload serializes the payload data with the correct serializers.

like image 808
tonycoco Avatar asked Apr 22 '14 04:04

tonycoco


2 Answers

DS.Store#pushPayload is able to take an array of items, not just one, and may contain side-loaded data. It processes a full payload and expects root keys in the payload:

{
  "posts": [{
    "id": 1,
    "title": "title",
    "comments": [1]
  }],
  "comments": [
    //.. and so on ...
  ]
}

DS.Store#push expects a single record which has been normalized and contains no side loaded data (notice there is no root key):

{
  "id": 1,
  "title": "title",
  "comments": [1]
}

For this reason, it makes sense for push to return the record, but for pushPayload to return nothing.

When you use pushPayload, a second lookup of store.find('post', 1) (or store.getById('post', 1)) is the way to go, I don't believe there is a better way.

like image 125
tstirrat Avatar answered Oct 17 '22 13:10

tstirrat


As of this PR pushPayload can now return an array of all the records pushed into the store, once the 'ds-pushpayload-return' feature flag has been enabled.

At the moment, this feature isn't available in a standard or beta release-- you'll have to use

"ember-data": "emberjs/data#master",

(i.e. Canary) in your package.json in order to access it. I'm not sure when the feature will be generally available.

like image 3
Max Wallace Avatar answered Oct 17 '22 13:10

Max Wallace