How to send POST instead of PUT or DELETE to Ember?

How to update or delete an entry using the POST verb using the Ember RESTAdapter? By default, it sends json using PUT or DELETE. Sending using these verbs is blocked for where I work.

I was hoping that I could do Rails when you send POST and tell if it is secretly PUT or DELETE using additional meta-information.

I am working with Ember 1.0.0 and ember-data 1.0.0beta2 via RESTAdapter.

+4
source share
3 answers

I think that overriding DS.RESTAdapter updateRecord and deleteRecord might 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"); } }); 
+8
source

You can override ajaxOptions in 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); } }); 
+2
source

This is the 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 }); } }) 

Note that changing type.typeKey to type.modelName

0
source

Source: https://habr.com/ru/post/1502623/


All Articles