[英]What is the right way to remove and re-add a polymer element instance?
Removing and re-adding a polymer element instance breaks the data bindings. 删除并重新添加聚合物元素实例会破坏数据绑定。 Calling prepareElement() reconnects everything, but is this the best way? 调用prepareElement()可以重新连接所有内容,但这是最好的方法吗?
Given this HTML, using the click-counter example 给定此HTML,使用点击计数器示例
<body>
<click-counter id="click_counter_id" count="5"></click-counter>
</body>
and the code 和代码
main() {
Logger.root.level = Level.ALL;
Logger.root.onRecord.listen((LogRecord rec) {
print('${rec.loggerName}: ${rec.level.name}: ${rec.time}: ${rec.message}');
});
initPolymer().run(() {
Polymer.onReady.then((_) {
var elem = querySelector('#click_counter_id');
var parent = elem.parent;
elem.remove();
new Timer(new Duration(seconds:1), () {
parent.children.add(elem);
// This reconnects the bindings. Is it correct?
(elem as PolymerElement).prepareElement();
});
});
});
}
everything seems ok. 一切似乎还好。 But then we get a warning logged: 但是随后我们收到一条警告记录:
polymer.events: FINE: event: [button].on-click => [click-counter].increment())
polymer.unbind: FINE: [click-counter] cancelUnbindAll
polymer.unbind: WARNING: [click-counter] already unbound, cannot cancel unbindAll
polymer.events: FINE: event: [button].on-click => [click-counter].increment())
Is there a better way to remove and re-add a polymer element instance? 是否有删除和重新添加聚合物元素实例的更好方法?
I have not tried it myself but it seems reasonable. 我自己还没有尝试过,但这似乎是合理的。 I hope the author doesn't mind coping his answer from Detached observables when re-using element. 我希望作者在重用元素时不介意从Detached observables中回答他。
Having looked into the polymer.js information bit I have found that there is a cancelUnbindAll function which must be called when the element is created or a preventDispose property set to true. 研究了polymer.js信息位后,我发现有一个cancelUnbindAll函数,在创建元素或将preventDispose属性设置为true时必须调用该函数。
For anyone that might need to do the same, in the Dart implementation you must call cancelUnbindAll in the leftView function after the super call, as follows: 对于可能需要执行此操作的任何人,在Dart实现中,必须在超级调用之后在leftView函数中调用cancelUnbindAll,如下所示:
void detached() {
super.leftView();
this.cancelUnbindAll(preventCascade: true);
}
Alternatively, you can simply override the preventDispose property in your custom element: 另外,您可以简单地在自定义元素中覆盖preventDispose属性:
bool get preventDispose => true;
There is also a preventDispose
see https://github.com/Polymer/polymer/issues/312 还有一个preventDispose
参见https://github.com/Polymer/polymer/issues/312
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.