如何在Ember 2.13.0中检测并保存关系更改? [英] How to detect and save relation change in Ember 2.13.0?

查看:88
本文介绍了如何在Ember 2.13.0中检测并保存关系更改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有文件清单.每个文档都有由 belongsTo 定义的客户属​​性.

I have documents list. Every document has client attribute defined by belongsTo.

当用户在其中一个文档中更改客户端时,我想在柜台中显示更改了多少文档.并且当用户决定他将按下发布"按钮时,该按钮会将客户端更改保存到api.

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.

DS.Model具有参数( https://emberjs.com/api/data/classes/DS.Model.html ):

DS.Model in ember 2.13 has parameters (https://emberjs.com/api/data/classes/DS.Model.html):

具有hasDirtyAttributes, dirtyType

hasDirtyAttributes, dirtyType

它们两者都不会对Ember design 所做的 Tos/HasMany 更改做出反应.我看到了很多有关此主题的答案,但是在2.13文档中没有看到任何用于模型的 isDirty()方法,也没有看到任何手动的 .send("becomeDirty")方法将文档模型设置为脏状态?我还看到了一些旧版本的Ember的插件/混合.

Both of them does not react on belongsTo/HasMany changes by Ember design. 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 ? I also saw several plugins/mixins for older Ember versions.

但是我的问题是,Ember创作者如何建议/咨询/最佳实践"来解决这个问题.是否有一些不需要任何第三方插件的基本方法/手动解决方案?就像使用onchange观察器来处理模型中的每个关系一样,还是使用具有@ each.dirtyType的计算属性来处理与子项相关的模型(甚至设置子项本身也不会被标记为脏对象?)?

But my question is, how do Ember creators "advise/advice/best practice" to deal with this. 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?) ?

在Ember 2.13中,什么是沙盒解决方案?

推荐答案

Ember(2.x)不会跟踪关系(例如hasMany),但是可以使用ember-addon ember-data-change-tracker几乎可以做吧.它允许您(自动)保存关系的当前状态,然后您可以将此已保存"(=旧状态)与当前状态进行比较.您必须自己找到一个区别.我的适配器的一个简单示例:

Ember (2.x) does not track relations (e.g. hasMany) but it is possible to use ember-addon ember-data-change-tracker that can almost do it. 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

这篇关于如何在Ember 2.13.0中检测并保存关系更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆