Last active
September 20, 2022 18:44
-
-
Save runspired/96618af26fb1c687a74eb30bf15e58b6 to your computer and use it in GitHub Desktop.
Useful Ember Data helpers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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
Thank you for those helpers :), they are very usefull to me. I can't remember why these won't be considered to be ember-data's built-in ?