Reputation: 5784
Is there a way with Ember JS to use the PATCH
verb in order to partially update a record on the server (instead of PUT
which will override the whole record).
Creating a record
Using POST
which is all good.
var car = store.createRecord('car', {
make: 'Honda',
model: 'Civic'
});
car.save(); // => POST to '/cars'
Modifying a record
Always using PUT
which is not ideal.
car.set('model', 'Accord')
car.save(); // => PUT to '/cars/{id}'
I would like to have the control over the HTTP verb used for saving.
Upvotes: 4
Views: 2481
Reputation: 1
you can use save()
in ember which uses the PATCH verb.
Records that already exist on the backend are updated using the HTTP PATCH verb.
store.findRecord('post', 1).then(function(post) {
post.get('title'); // => "Rails is Omakase"
post.set('title', 'A new post');
post.save(); // => PATCH to '/posts/1'
});
look for more details here
Upvotes: -2
Reputation: 37369
There is a way to do it, but you'll have to do a bit of work. Specifically, you'll need to override the updateRecord method in the adapter. Modifying the default implementation, you should come up with something like this:
export default DS.RESTAdapter.extend({
updateRecord(store, type, snapshot) {
const payload = {};
const changedAttributes = snapshot.changedAttributes();
Object.keys(changedAttributes).forEach((attributeName) => {
const newValue = changedAttributes[attributeName][1];
// Do something with the new value and the payload
// This will depend on what your server expects for a PATCH request
});
const id = snapshot.id;
const url = this.buildURL(type.modelName, id, snapshot, 'updateRecord');
return this.ajax(url, 'PATCH', payload);
}
});
You'll have to dig into the Snapshot documentation a bit to generate the request payload, but it shouldn't be too difficult.
Upvotes: 8