简体   繁体   English

灰烬数据快照以及如何检测更改?

[英]Ember Data Snapshot and how to detect changes?

I'm writing my own adapter/serializer. 我正在编写自己的适配器/序列化器。 In order to send data to backend, I have to detect changes in DS.Snapshot and original Ember object. 为了将数据发送到后端,我必须检测DS.Snapshot和原始Ember对象中的更改。 For ordinary attributes, it is possible to changedAttributes() but I did not found a way how to detect changes in hasMany relations. 对于普通属性,可以更改changeAttributes(),但是我没有找到一种方法来检测hasMany关系中的更改。

I can detect new relation using snapshot.hasMany('foo') and changedAttributes(). 我可以使用snapshot.hasMany('foo')和changedAttributes()来检测新关系。 But this approach is not able to find deleted relations. 但是这种方法无法找到已删除的关系。

Ember (2.x) does not track relations (eg hasMany) but it is possible to use ember-addon ember-data-change-tracker that can almost do it. Ember(2.x)不会跟踪关系(例如hasMany),但是可以使用几乎可以做到的ember-addon ember-data-change-tracker。 It allows you to (auto-)save the current state of relations and afterwards you can compare this 'saved' (=old state) with the current state. 它允许您(自动)保存关系的当前状态,然后您可以将此“已保存”(=旧状态)与当前状态进行比较。 You have to find a difference by yourself. 您必须自己找到一个区别。 A simple example from adapter: 适配器的一个简单示例:

snapshot.hasMany('users').length <-- current count of relations
snapshot.record.savedTrackerValue('users').length <-- old count of relations

Thanks to Christoper for pointing me to the right direction. 感谢Christoper为我指出正确的方向。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM