Data: How could I use put method to update a record instead of patch method

Created on 19 Oct 2015  路  6Comments  路  Source: emberjs/data

Most helpful comment

3099 has been merged and is available in canary behind the ds-improved-ajax feature flag.

Having this flag enabled, you can do this in your adapter to make a PUT instead of PATCH:

// app/adapters/application.js
import JSONAPIAdapter from "ember-data/adapters/json-api";

export default JSONAPIAdapter.extend({
  methodForRequest({ requestType }) {
    if (requestType === "updateRecord") {
      return "PUT";
    }

    return this._super(...arguments);
  }
});

All 6 comments

For now, I use the following hack. Is there a better way to do this?

'use strict';
import DS from 'ember-data';
import config from '../config/environment';

export default DS.JSONAPIAdapter.extend({
  // ...
  updateRecord: function (store, type, snapshot) {
    var data = {};
    var serializer = store.serializerFor(type.modelName);

    serializer.serializeIntoHash(data, type, snapshot, { includeId: true });

    var id = snapshot.id;
    var url = this.buildURL(type.modelName, id, snapshot, 'updateRecord');

    return this.ajax(url, 'PUT', { data: data });
  }
  // ...
});

3099 aims to allow easier overwriting of the made request.

3099 has been merged and is available in canary behind the ds-improved-ajax feature flag.

Having this flag enabled, you can do this in your adapter to make a PUT instead of PATCH:

// app/adapters/application.js
import JSONAPIAdapter from "ember-data/adapters/json-api";

export default JSONAPIAdapter.extend({
  methodForRequest({ requestType }) {
    if (requestType === "updateRecord") {
      return "PUT";
    }

    return this._super(...arguments);
  }
});

I am using emberjs 2.16 which does "options" method call for update out of the box (I have never seen options used anywhere else as rest API law). I am trying to have emberjs do all update calls to server as PUT.

Unfortunately both of these did not work for me

  updateRecord: function (store, type, snapshot) {
    var data = {};
    var serializer = store.serializerFor(type.modelName);

    serializer.serializeIntoHash(data, type, snapshot, { includeId: true });

    var id = snapshot.id;
    var url = this.buildURL(type.modelName, id, snapshot, 'updateRecord');

    return this.ajax(url, 'PUT', { data: data });
  }

And

methodForRequest({ requestType }) {
    if (requestType === "updateRecord") {
      return "PUT";
    }

    return this._super(...arguments);
  }

Now I can not move any further.
I am updating from controller with the following save action code

    save() {
      this.get('model').save().then(
        () => this.transitionToRoute('calendars'),
        () => console.log('error saving')
      );
    },

The overriding-updateRecord-approach works for me, see this twiddle... 馃 do you spot any differences to your code?

works thanks

Was this page helpful?
0 / 5 - 0 ratings