Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I send POST in place of PUT or DELETE in Ember?

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.

like image 686
Greg Malcolm Avatar asked Dec 20 '22 00:12

Greg Malcolm


2 Answers

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");
  }
});
like image 146
Marcio Junior Avatar answered Jan 09 '23 08:01

Marcio Junior


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);
    }
});
like image 21
dogawaf Avatar answered Jan 09 '23 10:01

dogawaf