This repository has been archived by the owner on Mar 13, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 42
/
observing-changes-to-light-dom-children.html
84 lines (74 loc) · 2.87 KB
/
observing-changes-to-light-dom-children.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<!--
Copyright (c) 2014 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
-->
<!--
# Observing Changes to Light DOM Children
Shows how to use a **MutationObserver** to be notified when nodes are
added to or removed from the light DOM.
Polymer adds an **onMutation()** callback to every element. The callback takes
two arguments, a DOM element to observe, and a callback that takes the
MutationObserver object and the list of **MutationRecord** objects as
arguments.
Polymer('my-element', {
message: '',
ready: function() {
this.onMutation(this, this.childrenUpdated);
},
childrenUpdated: function(observer, mutations) {
this.message = "New <div> with text '" +
mutations[0].addedNodes[0].textContent +
"' added to light DOM.";
// Monitor again.
this.onMutation(this, this.childrenUpdated);
},
...
});
In this example, the user taps on a `<button>` to add a `<div>` to the
element's light DOM. Adding a node triggers the mutation observer. Note
the use of a **MutationRecord** to get information about the newly appended
light DOM node:
childrenUpdated: function(observer, mutations) {
...
mutations[0].addedNodes[0].textContent
...
}
Read the
[official documentation for Mutation Observer and the onMutation callback](http://www.polymer-project.org/resources/faq.html#mutationlightdom).
Read the
[MDN documentation for Mutation Observer](https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver).
[jsbin](http://jsbin.com/wifiyo/edit)
-->
<link rel="import" href="../../components/polymer/polymer.html">
<polymer-element name="my-element">
<template>
<div>{{message}}</div>
<button on-tap="{{addDivToLightDom}}">Add div to light DOM</button>
<content></content>
</template>
<script>
Polymer({
message: '',
ready: function() {
// Observe a single change.
this.onMutation(this, this.childrenUpdated);
},
childrenUpdated: function(observer, mutations) {
this.message = "New <div> with text '" +
mutations[0].addedNodes[0].textContent +
"' added to light DOM.";
// Monitor again.
this.onMutation(this, this.childrenUpdated);
},
addDivToLightDom: function(e) {
var newDiv = document.createElement('div');
newDiv.textContent = 'I am new';
this.appendChild(newDiv);
}
});
</script>
</polymer-element>