EmberJs,changedAttributes()没有显示Hash属性的嵌套键中的更改 [英] EmberJs, changedAttributes() is not showing changes in the nested keys of a Hash attribute
问题描述
对象是DS.Model:
The object is DS.Model:
$E.toString()
> "<reports-dashboard-client-app@model:report::ember596:914fc1b0-b14d-0133-bce2-68a86d03d830>"
属性是一个哈希:
// app/models/report.js
export default DS.Model.extend({
filters: DS.attr(),
...
});
这是现在的样子:
$E.get('filters')
> Object {__ember_meta__: Meta}
> __ember_meta__: Meta
> age_groups: (...)
> get age_groups: GETTER_FUNCTION()
> set age_groups: SETTER_FUNCTION(value)
> genders: (...)
> get genders: GETTER_FUNCTION()
> set genders: SETTER_FUNCTION(value)
> __proto__: Object
模型中没有任何变化:
$E.changedAttributes()
> ember$data$lib$system$empty$object$$EmptyObject {}
我修改Hash键,但在型号中仍然没有更改:
I modify one of the Hash keys but still not change in the Model:
$E.get('filters.age_groups')
> ["21-30", "31-40"]
$E.set('filters.age_groups', ["21-30"])
> ["21-30"]
$E.changedAttributes()
> ember$data$lib$system$empty$object$$EmptyObject {}
只有我更改Hash it self我在模型中获得更改:
Only if I change the Hash it self I obtain a change in the Model:
$E.set('filters', {})
> Object {}
$E.changedAttributes()
> ember$data$lib$system$empty$object$$EmptyObject {filters: Array[2]}
什么是好奇的是,我可以观察属性 filters.age_groups
,并且当该键有变化时观察者被正确触发。
What is curious is that I can observe the property filters.age_groups
and the Observer is triggered properly when this key has a change.
如何检查DS.Model属性的嵌套键是否有任何更改?H $?
How can I check if there is any change in the nested keys of DS.Model attribute which is a Hash?
推荐答案
最近我面对同样的问题,我通过使用 Ember Fragments 。
I faced the same issue recently, and I solved by using Ember Fragments.
您仍然需要为每个关系创建模型/片段,但样本很容易理解。
You still needs to create model/fragments for each relationship, but the samples are quite easy to understand.
如果您需要帮助,请留下评论。
Please leave comments if you need help though.
更新
另一种解决方法是使用 EmbeddedMixin
There's another way of solving to use EmbeddedMixin
参考:< a href =http://discuss.emberjs.com/t/hasdirtyattributes-always-false-for-nested-properties/8534 =nofollow noreferrer> http://discuss.emberjs.com/t/hasdirtyattributes -always-false-for-nested-properties / 8534
这篇关于EmberJs,changedAttributes()没有显示Hash属性的嵌套键中的更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!