Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSONAPI store.normalize doesn't accept "data:{}" format #4285

Closed
orestis opened this issue Mar 29, 2016 · 5 comments
Closed

JSONAPI store.normalize doesn't accept "data:{}" format #4285

orestis opened this issue Mar 29, 2016 · 5 comments

Comments

@orestis
Copy link

orestis commented Mar 29, 2016

I thought there was a bug in normalize, but @wecc pointed to me that store.normalize expects its payload to not be wrapped in a data:{...} block. Passing it a JSONAPI server's response directly results in a weird exception when trying to deCamelize.

I believe there should either be a warning when the data seems not-correctly structured, or since the usefulness of being able to pass an unmodified server response directly to normalize (and then to store.push), it should be able to handle both cases.

I am aware of the pushPayload (and the relevant feature flag in 2.5 to return values from it) but it would be nice if this worked too.

Original (now working) Twiddle here: https://ember-twiddle.com/f57e4bff221997a87f51?openFiles=templates.application.hbs%2C

@pangratz
Copy link
Member

I am aware of the pushPayload (and the relevant feature flag in 2.5 to return values from it) but it would be nice if this worked too.

There was a PR which introduced store.normalizePayload #3838, but it has been closed since we thought that the ds-pushpayload-return feature would be sufficient.

Can you outline the use case for which you would need a method, which normalizes the payload but does not push it into the store?

@orestis
Copy link
Author

orestis commented Mar 31, 2016

That is a fair question - I don't have an answer. This mainly stem because my back-end always wraps things with data. The documentation states:

push is used to notify Ember Data's store of new or updated records that exist in the backend. This will return a record in the loaded.saved state. The primary use-case for store#push is to notify Ember Data about record updates (full or partial) that happen outside of the normal adapter methods (for example SSE or Web Sockets).

normalize converts a json payload into the normalized form that push expects.

And adds the example that I missed all along (that you have to extract .data). This is more a documentation issue now, because I'm not sure where I could get some payload that a) needs normalization through the adapter and b) doesn't come from my back-end (which does return things in a JSONAPI format). Other people might have more niche use-cases though.

@bmac
Copy link
Member

bmac commented Mar 31, 2016

@wecc what are your thoughts on the idea of making normalize smart enough to unwrap the object from if its nested under a data key?

@pangratz
Copy link
Member

FYI, RFC#161 proposes to have a public store.normalizePayload()...

@runspired
Copy link
Contributor

Given the ease of composing a few store methods to properly normalize data with better context than store.normalize is capable of, I'm going to close this.

As an example:

const modelName = 'user';
const ModelClass = store.modelFor(modelName);
const serializer = store.serializerFor(modelName);
const normalized = serializer.normalizeResponse(store, ModelClass, rawPayload, null, 'query');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants