【发布时间】:2016-06-07 08:09:37
【问题描述】:
当我通过const myRecord = this.store.createRecord('myType', myObject) 创建记录然后myRecord.save() 请求通过适配器发送到服务器。数据保存成功,服务器将所有数据返回给客户端。它通过序列化程序通过normalize() 钩子返回。
问题在于 Ember 不会使用从服务器返回的属性更新 myRecord 对象,例如 id 或 version ...
当我刷新页面时,所有属性都在那里(当然)。
我在更新记录时遇到了类似的问题。我的应用程序中的每条记录都有一个版本属性,由服务器检查。该版本在每次保存时递增。这是为了数据安全。问题是当我尝试多次更新记录时,只有第一次尝试成功。原因是在请求从服务器返回后,version 没有更新。 (是的,服务器返回更新版本)
这对我来说是一个令人惊讶的行为,在我看来,这就像这里建议的预期功能 - https://github.com/ebryn/ember-model/issues/265。 (但该帖子来自 2013 年,建议的解决方案对我不起作用)。
有什么想法吗?
为了完整起见,这里是相关代码(简化和重命名)
我的模型
export default Ember.Model.extend({
typedId: attr('string') // this serves as an ID
version: attr('string'),
name: attr('string'),
markup: attr('number'),
});
我的适配器
RESTAdapter.extend({
createRecord(store, type, snapshot) {
// call to serializer
const serializedData = this.serialize(snapshot, options);
const url = 'http://some_internal_api_url';
// this returns a promise
const result = this.ajax(url, 'POST', serializedData);
return result;
},
});
mySerializer
JSONSerializer.extend({
idAttribute: 'typedId',
serialize(snapshot, options) {
var json = this._super(...arguments);
// perform some custom operations
// on the json
return json;
},
normalize(typeClass, hash) {
hash.data.id = hash.data.typedId;
hash.data.markup = hash.data.attribute1;
hash.data.version = parseInt(hash.data.version, 10);
return hash;
}
});
【问题讨论】:
标签: ember.js ember-data