Ember JS,补丁记录REST适配器

时间:2015-07-08 15:28:36

标签: javascript rest ember.js

有没有办法让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动词。

2 个答案:

答案 0 :(得分:5)

有办法做到这一点,但你必须做一些工作。具体来说,您需要覆盖适配器中的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文档以生成请求有效负载,但这不应该太困难。

答案 1 :(得分:-1)

你可以在使用PATCH动词的ember中使用store.findRecord('post', 1).then(function(post) { post.get('title'); // => "Rails is Omakase" post.set('title', 'A new post'); post.save(); // => PATCH to '/posts/1' }); 。 使用HTTP PATCH谓词更新后端已存在的记录。

private String GENERATEGROUPKEY()
{
    /* `out` is a PrintWriter using the sockets output stream */
    out.println("GENERATEGROUPKEY");

    try
    {
        /* `in` is a BufferedReader using the sockets input stream */
        String response = in.readLine();
        String[] temp = response.split(" ");

        return temp[1];
    }
    catch (IOException ex)
    {
        return null; // throw connection error to client
    }
}

查找更多详情here

相关问题