简体   繁体   English

如何在Ember 2.13.0中检测并保存关系变化?

[英]How to detect and save relation change in Ember 2.13.0?

I have documents list. 我有文件清单。 Every document has client attribute defined by belongsTo . 每个文档都有belongsTo定义的客户端属性。

When user change client in one of documents i want to show in the counter how many documents are changed. 当用户在其中一个文档中更改客户端时,我想在计数器中显示更改了多少文档。 And when user decide he will press "publish" button which will save documents client changes to api. 当用户决定按“发布”按钮时,会将文档客户端更改保存到api。

DS.Model in ember 2.13 has parameters ( https://emberjs.com/api/data/classes/DS.Model.html ): ember 2.13中的DS.Model有参数( https://emberjs.com/api/data/classes/DS.Model.html ):

hasDirtyAttributes, dirtyType hasDirtyAttributes,dirtyType

Both of them does not react on belongsTo/HasMany changes by Ember design . 他们两个都没有通过Ember 设计belongsTo / HasMany的变化作出反应。 I saw many answers to this topic, but I do not see any isDirty() method for model in 2.13 documentation, nor any .send("becomeDirty") method to manually set document model in dirty status ? 我看到了很多关于这个主题的答案,但我没有在2.13文档中看到任何isDirty()方法,也没有看到任何.send(“becomeDirty”)方法来手动设置脏状态的文档模型? I also saw several plugins/mixins for older Ember versions. 我还看到了几个旧版Ember版本的插件/ mixins。

But my question is, how do Ember creators "advise/advice/best practice" to deal with this. 但我的问题是,Ember创作者如何“建议/建议/最佳实践”来解决这个问题。 Is there some basic way/manual solution that does not require any third party addon ? 是否有一些基本的方法/手动解决方案,不需要任何第三方插件? Like with maybe onchange observer for every relation in model?, or computed property with @each.dirtyType for child related models (or even set children will not be flagged as dirty itself?) ? 对于模型中的每个关系可能都是onchangechange observer,或者对于子相关模型使用@ each.dirtyType计算属性(或者甚至设置子项不会被标记为脏本身?)?

What is sandbox solution for this in Ember 2.13 ? 在Ember 2.13中,什么是沙箱解决方案?

It's been some time. 已经有一段时间了。 For has-many I use this solution. 对于has-many,我使用这个解决方案。 'isTasksDirty' will get back to false if user change has-many relation to the same group of items as before: 如果用户更改与以前相同的项目组有多个关系,'isTasksDirty'将返回false:

/* RELATIONS DIRTINESS */
    isRelationDirty: Ember.computed(
        'isDepartmentsDirty',
        'isTasksDirty'
        {
            get(key) {
                return this.get("isDepartmentsDirty") ||
                       this.get("isTasksDirty");
            },
            set(key, value) {
                this.set("isDepartmentsDirty", value);
                this.set("isTasksDirty", value);
            }
        }
    ),

    isTasksDirty:false,
    tasksChanged: Ember.observer('tasks.[]', function() {
        if(!arraysEqual(this.get("tasks").content.currentState, this.get("tasks").content.canonicalState)){
        this.set("isTasksDirty", true);
    } else {
        this.set("isTasksDirty", false);
    }
}),

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 my adapter: 我的适配器的一个简单示例:

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

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

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