I'm looking to override createRecord for a model so that I can extract a HTML form value and append it to the JSON body prior to the POST to the REST endpoint.
I'm trying to avoid including the field in the model definition as it is only used once (on create) and never returned from the REST endpoint nor needed when updating a record.
e.g.
{
"id": "55fd0ff81be9be006737acef",
"name": "Trujillo",
"email": "trujillotyson@dentrex.com",
"createdDate": "2014-05-25T01:59:56 -08:00",
// Append this to the model JSON
"stripe_token": "55fd0ff8f342d8ba581a9e06"
}
However, it's not clear as to how to override this—the official docs have some thin documentation in the related serialize method, but it's not comprehensive.
Where possible I want to lean on the existing method to handle returning the Promise and only append this field.
/// app/adapters/some-model.js
export default ApplicationAdapter.extend({
createRecord(store, type, snapshot) {
var data = this.serialize(snapshot, { includeId: true });
// How do we achieve this - appending a key with
// data from the HTML form that `save()` is called from?
data.set('stripe_token', this.get('stripeToken'));
// Correct? Still leverage the base method
// but with our modifed 'data' instead
return this._super(store, type, data)
}
});
How can I achieve the above?
Aucun commentaire:
Enregistrer un commentaire