我想知道您是否可以监听UListElement
的元素何时发生了变化(即添加或删除了LIElement
)?
UListElement toDoList = query('#to-do-list');
LIElement newToDo = new LIElement();
newToDo.text = "New Task";
toDoList.elements.add(newToDo);
// how do I execute some code once the newToDo has been added to the toDoList?
我假设elements.add()
是异步的,因为我来自ActionScript背景。这是一个有效的假设吗?
更高级别的框架为您提供事件,但在HTML5 API级别,您必须使用MutationObserver。
这里有一个示例,其中可以在任何DOM元素对象上设置突变观测器。然后,处理程序可以根据需要处理突变事件。
void setMutationObserver(Element element){
new MutationObserver(mutationHandler)
.observe(element, subtree: true, childList: true, attributes: false);
}
void mutationHandler(List<MutationRecord> mutations,
MutationObserver observer){
for (final MutationRecord r in mutations){
r.addedNodes.forEach((node){
// do stuff here
});
r.removedNodes.forEach((node){
// or here
});
}
}
element.elements.add(otherElement)
是同步的,相当于Javascript中的element.appendChild(otherElement)
(即DOM Level 2 Core:appendChild)。
您可能也对我们与MDV:的合作感兴趣
- http://blog.sethladd.com/2012/11/your-first-input-field-binding-with-web.html
- http://blog.sethladd.com/2012/11/your-first-model-driven-view-with-dart.html