json - ember hasDirtyAttributes 不适用于对象 (json)
问题描述
我有一个模型:
export default Model.extend({
title: attr('string'),
attributes: attr('jsonb')
});
存储在 Postgresattributes
中的自定义文件在哪里。json
jsonb
让我们说:
{
"name":"Bob",
"city":""
}
所以我可以很容易地attributes
使用模板
<form.element .. @property="attributes.city"/>
或model.set('attributes.city','city name')
问题:hasDirtyAttributes
不要改变,因为技术上我们有旧对象。但是当我尝试复制对象时,让说
JSON.parse(JSON.stringify(this.get('attributes'))
hasDirtyAttributes
按预期工作
那么如何为Mixin
任何Model
属性更改attribute
将标记hasDirtyAttributes
为true
. 我将更新整个对象,因此实际上更改了哪个属性并不重要。
同样的问题:https ://discuss.emberjs.com/t/hasdirtyattributes-do-not-work-with-nested-attributes-json-api/15592
现有的解决方案对我根本不起作用:
- 余烬更脏
- ember 数据关系跟踪器
- ember-data-model-fragments(引擎盖下的很多变化并破坏了我的应用程序)
更新:
一些不完美的想法有助于更好地描述我想要实现的目标:假设我们将观察者添加到任何对象fileds
:
export default Model.extend({
init: function(){
this._super();
this.set('_attributes', Object.assign({}, this.get('attributes'))); //copy original
Object.keys(this.get('attributes')).forEach((item) => {
this.addObserver('attributes.'+ item, this, this.objectObserver);
});
}
...
})
和观察者:
objectObserver: function(model, filed){
let privateFiled = '_' + filed;
if (model.get(privateFiled) != model.get(filed)) { //compare with last state
model.set(privateFiled, this.get(filed));
model.set('attributes', Object.assign({}, this.get('attributes')) );
}
}
这是可行的,但是当我由于复制对象而filed
在每个文件上再次流线型时更改了一个。所以在这个关键改变每一个我标记观察归档为object
objectObserver
filed
object
dirty
解决方案
进一步的ember
发展将减少事件监听器和双向绑定的使用,实际上 Glimmer 组件仅支持One-way Data Flow
. ember
因此,在这种情况下,对使用单向数据流的未来版本友好是一种很好的方法。所以就我而言,当我使用ember boostrap
解决方案时,看起来像
<form.element @controlType="textarea" @onChange={{action 'attributeChange'}}
行动在哪里attributeChange
都能发挥作用。
新的 Glimmer / Octane 样式基于modifier
并且看起来像:
{{!-- templates/components/child.hbs --}}
<button type="button" {{on "click" (fn @onClick 'Hello, moon!')}}>
Change value
</button>
推荐阅读
- python - 如何在没有 read_only = True 的情况下使用 openpyxl 加载和更新大于 15MB 的大型 excel 文件
- fonts - 如何在 VSCode 集成终端中更改文本的字体颜色?
- spring - 在单元测试类中找不到spring应用程序类
- c# - 将参数与 Func<> 一起使用
- text - 如何从文本文件中提取字符
- apache - Htaccess:重定向到目录中的第一个或唯一一个文件
- r - 当涉及到 R 中的两位数时,比较函数(==,all.equal)不能正常工作?
- python - 未识别所有数据类型
- javascript - 连接两个字符串并获取剪贴板中的值
- xml - 错误:对实体“名称”的引用必须以“;”结尾 分隔符