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 badges
Add a ment  | 

3 Answers 3

Reset to default 9

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

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