How does one update or delete a record using the POST verb using the Ember RESTAdapter? By default it sends the json using PUT or DELETE verbs. Sending using these verbs is blocked for where I'm working at.
I was kind of hoping I could do the Rails thing where you send a POST and tell it whether it's secretly a PUT or DELETE using extra meta information.
I'm working with Ember 1.0.0 and ember-data 1.0.0beta2 through the RESTAdapter.
I think that overriding the DS.RESTAdapter
updateRecord
and deleteRecord
could work:
DS.RESTAdapter.reopen({
updateRecord: function(store, type, record) {
var data = {};
var serializer = store.serializerFor(type.typeKey);
serializer.serializeIntoHash(data, type, record);
var id = Ember.get(record, 'id');
return this.ajax(this.buildURL(type.typeKey, id), "POST", { data: data });
},
deleteRecord: function(store, type, record) {
var id = Ember.get(record, 'id');
return this.ajax(this.buildURL(type.typeKey, id), "POST");
}
});
You can override ajaxOptions on RESTAdapter:
DS.RESTAdapter.reopen({
ajaxOptions: function(url, type, hash) {
hash = hash || {};
if (type === 'PUT' || type === 'DELETE') {
hash.data = hash.data || {};
hash.data['_method'] = type;
type = 'POST';
}
return this._super(url, type, hash);
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With