admin管理员组文章数量:1315101
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.
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.
Share Improve this question edited Dec 24, 2016 at 14:11 Marcio Junior 19.1k4 gold badges46 silver badges47 bronze badges asked Sep 17, 2013 at 14:06 Greg MalcolmGreg Malcolm 3,4274 gold badges24 silver badges24 bronze badges3 Answers
Reset to default 9I 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);
}
});
This is syntax for Ember 2.7.3 and Ember Data 2.7.0
export default DS.RESTAdapter.extend({
updateRecord: function(store, type, snapshot) {
let id = snapshot.id;
let data = this.serialize(snapshot, { includeId: true });
const urlForQueryRecord = this.buildURL(type.modelName, id, snapshot, 'updateRecord');
return this.ajax(urlForQueryRecord, 'POST', { data: data });
}
})
Please notice the change of type.typeKey
to type.modelName
本文标签: javascriptHow do I send POST in place of PUT or DELETE in EmberStack Overflow
版权声明:本文标题:javascript - How do I send POST in place of PUT or DELETE in Ember? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741971434a2407861.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论