gpt4 book ai didi

javascript - 如果它是可变单元格,如何在 didReceiveAttrs Hook 中获取真实的 attr 值

转载 作者:行者123 更新时间:2023-11-30 15:39:55 27 4
gpt4 key购买 nike

请参阅下面的示例代码。假设我有一个组件 my-text-box

我的文本框.hbs

{{my-text-box value=modelName}}

我想通过 didReceiveAttrs 钩子(Hook)而不是 observers 来观察 value 属性的变化以获得更好的性能。

我的文本框.js

didReceiveAttrs: function(args) {
if (args.oldAttrs == null) {
// This is the initial render, but I don't need to anything here.
} else if (args.newAttrs.value != args.oldAttrs.value) {
// supposed `value` was changed from outside, I can do something here...

// BUT, `args.newAttrs.value` is not always the prop value,
// `Ember` would automatically wrap it with `{{mut}}` helper.
// The data structure would be:
// {
// value: value,
// update: function (val) {
// source.setValue(val);
// }
// }
}
}

我想要的是我不必关心 attr 值是否是一个 mutable 单元格,我应该得到一个总能获得真实值的方法。我看到有一个 HTMLBars hook ember-htmlbars/hooks/get-value 但它没有公开给公共(public) API。我在想的是,也许 Ember 应该同时更改 newAttrsoldAttrs 以具有直接值而不是那些 mutable 对象。

有人有办法处理吗?谢谢!

最佳答案

我的建议是自己存储之前的值,然后比较:

export default Ember.Component.extend({
_previousAttr: null,

didReceiveAttrs() {
this._super(...arguments);
if (this.get('_previousAttr') !== this.get('attrName')) {
this.set('_previousAttr', this.get('attrName'));
// do your thing
}
}
});

关于javascript - 如果它是可变单元格,如何在 didReceiveAttrs Hook 中获取真实的 attr 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40974509/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com