Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How could I use put method to update a record instead of patch method #3870

Closed
zachzhao1984 opened this issue Oct 19, 2015 · 6 comments
Closed

Comments

@zachzhao1984
Copy link

No description provided.

@SeyZ
Copy link

SeyZ commented Oct 21, 2015

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

@pangratz
Copy link
Member

#3099 aims to allow easier overwriting of the made request.

@pangratz
Copy link
Member

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

@SaimonL
Copy link

SaimonL commented Oct 13, 2017

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

@pangratz
Copy link
Member

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

@SaimonL
Copy link

SaimonL commented Oct 13, 2017

works thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants