我有一个自定义的聚合物元素,带有已发布的属性inputValue
。
该组件侦听inputValue
更改,并在更改时将新值报告给内部逻辑。
相反的方向也是如此:元素监听内部逻辑的变化,并将新值设置为inputValue
。
当新值设置为inputValue
时,这会使观察系统通知所有侦听器,但元素本身就是侦听器。
我希望避免通知元素自己所做的更改。
下面是自定义元素Dart代码:
@CustomTag("codemirror-input")
class CodemirrorInput extends PolymerElement {
@published
String mode;
@published
String inputValue;
CodemirrorInput.created() : super.created();
void ready() {
Map options = {
'mode': mode
};
GradeCodeMirror editor = new GradeCodeMirror.fromElement(
$['codemirror'], options: options);
onPropertyChange(this, #inputValue, (){
editor.setOption("value", inputValue);
});
editor.onChange.listen((_){
inputValue = editor.getDoc().getValue();
});
}
}
发布于 2014-10-29 15:54:06
我想知道如果值实际上没有改变,为什么inputValueChanged
会触发,我认为只有当旧值和新值不同时它才会触发。但是如果它真的不能以这种方式工作,你可以自己检查
editor.onChange.listen((_){
var val = editor.getDoc().getValue();
if(inputValue != val) {
inputValue = val;
}
});
onPropertyChange(this, #inputValue, (){
editor.setOption("value", inputValue);
});
可以通过向元素添加一个方法来简化
void inputValueChanged(oldValue, newValue) {
editor.setOption("value", inputValue);
}
每次inputValue
更改时都会调用此方法。
https://stackoverflow.com/questions/26633540
复制相似问题