Ember JS,补丁记录REST适配器

Gab*_*Gab 3 javascript rest ember.js

有没有办法让Ember JS使用PATCH动词来部分更新服务器上的记录(而不是PUT覆盖整个记录).

创建记录

使用POST哪个都很好.

var car = store.createRecord('car', {
  make: 'Honda',
  model: 'Civic'
});
car.save(); // => POST to '/cars'
Run Code Online (Sandbox Code Playgroud)

修改记录

总是使用PUT哪个并不理想.

car.set('model', 'Accord')
car.save(); // => PUT to '/cars/{id}'
Run Code Online (Sandbox Code Playgroud)

我想控制用于保存的HTTP动词.

GJK*_*GJK 7

有办法做到这一点,但你必须做一些工作.具体来说,您需要覆盖适配器中的updateRecord方法.修改默认实现,你应该想出这样的东西:

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);
    }
});
Run Code Online (Sandbox Code Playgroud)

您需要深入了解Snapshot文档以生成请求有效负载,但这不应该太难.