我正在使用 ember 2.18,其中更新请求是 PATCH。但是后台是SAILS,不支持PATCH。因此,我必须将补丁请求转换为 PUT。
我看到了this问题,人们似乎已经在那里解决了。但这对我不起作用。代码如下:
import App from './../app';
import DS from "ember-data";
import { computed } from "@ember/object";
import { camelize } from '@ember/string';
import JSONAPIAdapter from "ember-data/adapters/json-api";
export default DS.JSONAPIAdapter.extend({
coalesceFindRequests: true,
host: App.GPT.Configuration.restServer,
methodForRequest: ({ requestType }) => {
console.log('Log')
if (requestType === "updateRecord") {
return "PUT";
}
return this._super(...arguments);
},
pathForType(type) {
return camelize(type) + 's';
},
headers: computed(function () {
if (!App.StoreUtil.getSessionId()) {
if (App.GPT.ApplicationController) {
App.GPT.ApplicationController.set("hasMessages", [
{
message: "Session expired."
}
]);
App.GPT.ApplicationController.transitionToRoute("/");
} else {
window.location = "/";
}
}
return {
sid: App.StoreUtil.getSessionId()
};
}).volatile()
});
将请求转换为 PUT 的代码是:
methodForRequest: ({ requestType }) => {
console.log('Log')
if (requestType === "updateRecord") {
return "PUT";
}
return this._super(...arguments);
}
但是,根本没有调用该方法。有趣的是每个请求都会调用 pathForType。
最佳答案
methodForRequest
仅在您使用 ember-data 2.8
+ 并且您启用了 ds-improved-ajax
标志时可用环境.js
您的另一个选择是覆盖 updateRecord()
关于javascript - Ember 将 PATCH 调用转换为 PUT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49443294/