Search code examples
javascriptjsonember.jsember-data

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.


Solution

  • 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");
      }
    });