Ember JS 有没有办法使用 PATCH
动词来部分更新服务器上的记录(而不是 PUT
将覆盖整个记录) .
创建记录
使用 POST
一切都很好。
var car = store.createRecord('car', {
make: 'Honda',
model: 'Civic'
});
car.save(); // => POST to '/cars'
修改记录
总是使用 PUT
这并不理想。
car.set('model', 'Accord')
car.save(); // => PUT to '/cars/{id}'
我想控制用于保存的 HTTP 动词。
最佳答案
有一种方法可以做到,但您必须做一些工作。具体来说,您需要覆盖 updateRecord适配器中的方法。修改 default implementation ,你应该想出这样的东西:
export default DS.RESTAdapter.extend({
updateRecord(store, type, snapshot) {
const payload = {};
const changedAttributes = snapshot.changedAttributes();
Object.keys(changedAttributes).forEach((attributeName) => {
const newValue = changedAttributes[attributeName][1];
// Do something with the new value and the payload
// This will depend on what your server expects for a PATCH request
});
const id = snapshot.id;
const url = this.buildURL(type.modelName, id, snapshot, 'updateRecord');
return this.ajax(url, 'PATCH', payload);
}
});
您必须深入研究 Snapshot生成请求负载的一些文档,但应该不会太难。
关于javascript - Ember JS,补丁记录 REST 适配器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31297152/