Skip to content

Instantly share code, notes, and snippets.

@alexspeller
Forked from runspired/push-deletion.js
Created May 9, 2018 22:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexspeller/aa74b2080a1a6308ca5d5ecec4a6975c to your computer and use it in GitHub Desktop.
Save alexspeller/aa74b2080a1a6308ca5d5ecec4a6975c to your computer and use it in GitHub Desktop.
Useful Ember Data helpers
/*
notifying the store that a record has been remotely deleted and should be fully removed.
*/
function pushDeletion(store, type, id) {
let record = store.peekRecord(type, id);
if (record !== null) {
let relationships = {};
let hasRelationships = false;
record.eachRelationship((name, { kind }) => {
hasRelationships = true;
relationships[name] = {
data: kind === 'hasMany' ? [] : null
};
});
if (hasRelationships) {
store.push({
data: {
type,
id,
relationships
}
});
}
record.unloadRecord();
}
}
/*
Avoids common pitfalls and the weird undocumented special-sauce format that must be used with the built in pushPayload
*/
function pushPayload(store, modelName, rawPayload) {
let ModelClass = store.modelFor(modelName);
let serializer = store.serializerFor(modelName);
let jsonApiPayload = serializer.normalizeResponse(store, ModelClass, rawPayload, null, 'query');
return store.push(jsonApiPayload);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment