请参阅下面的示例代码。由于我有一个组件my-text-box
如何在didReceiveAttrs钩子中获得真实的attr值,如果它是可变的单元格
my-text-box.hbs
{{my-text-box value=modelName}}
我想通过didReceiveAttrs
挂钩,而不是observers
观察value
属性的变化有更好的表现。
my-text-box.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
但它没有暴露给公共API。而我在想的是,也许Ember
应该将newAttrs
和oldAttrs
都改为具有直接值而不是那些mutable
对象。
有没有人有办法处理?谢谢!
'newAttrs'和'oldAttrs'不是公共API:X – locks
所以很难替换'observers',我需要知道一个属性是否被更改。 http://emberjs.com/api/classes/Ember.Component.html#event_didReceiveAttrs – shijistar