为了在孩子和父母之间交流,事件似乎是最优雅的方式。父母与孩子之间的沟通有哪些选择?
更具体地说,当子方法变得可见时,我想要它调用它。这些都是我想出的点子:
还有其他选择吗?
发布于 2013-12-02 12:28:37
我还没有试过,但是也许MutationObserver能做你想做的事。
Seth的聚合物示例包含两个示例:第一个例子是onMutation事件element.dart
library my_element;
import 'package:polymer/polymer.dart';
import 'dart:html';
import 'dart:async';
@CustomTag('my-element')
class MyElement extends PolymerElement {
MyElement.created() : super.created() {
// NOTE this only fires once,
// see https://groups.google.com/forum/#!topic/polymer-dev/llfRAC_cMIo
// This is useful for waiting for a node to change in response
// to some data change. Since we don't know when the node will
// change, we can use onMutation.
onMutation($['list']).then((List<MutationRecord> records) {
$['out'].text = 'Change detected at ${new DateTime.now()}';
});
new Timer(const Duration(seconds:1), () {
$['list'].children.add(new LIElement()..text='hello from timer');
});
}
}
第二个示例使用MutationObserver类element.dart
===编辑===
你试过链接的例子了吗?“观察”方法允许指定应观察的内容:
/**
* Observes the target for the specified changes.
*
* Some requirements for the optional parameters:
*
* * Either childList, attributes or characterData must be true.
* * If attributeOldValue is true then attributes must also be true.
* * If attributeFilter is specified then attributes must be true.
* * If characterDataOldValue is true then characterData must be true.
*/
void observe(Node target,
{bool childList,
bool attributes,
bool characterData,
bool subtree,
bool attributeOldValue,
bool characterDataOldValue,
List<String> attributeFilter}) {
发布于 2015-07-19 06:07:44
为了从父母的聚合物到儿童的聚合物,这个解决方案对我有好处。
如果我们有这样的儿童聚合物元素:
library my_element;
import 'package:polymer/polymer.dart';
import 'dart:html';
import 'dart:async';
@CustomTag('my-element')
class MyElement extends PolymerElement {
MyElement.created() : super.created() {
}
myCustomMethod(param){
print("pass-in param = $param");
}
}
要从父元素访问子元素,请执行以下操作:
(theParentClass.querySelector("my-element") as MyElement).myCustomMethod({"done":true});
https://stackoverflow.com/questions/20327749
复制相似问题