You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/connectedCallback.md
+64-19Lines changed: 64 additions & 19 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,28 +3,73 @@
3
3
4
4
@description A lifecycle hook called after the component's element is inserted into the document.
5
5
6
-
@signature`connectedCallback: function () { ... }`
6
+
@signature`connectedCallback: function (element) { ... }`
7
7
8
-
Mainly used as the context to orchestrate property bindings that would
9
-
otherwise be a stream or an inappropriate side-effect during a getter.
8
+
Use to orchestrate property bindings that would
9
+
otherwise be a stream or an inappropriate side-effect during a getter.
10
10
11
-
For example, the following listens to changes on the `name` property
12
-
and counts them in the `nameChanged` property:
11
+
For example, the following listens to changes on the `name` property
12
+
and counts them in the `nameChanged` property:
13
+
14
+
```html
15
+
<my-component></my-component>
16
+
17
+
<scripttype="module">
18
+
import {Component} from"can";
19
+
20
+
Component.extend({
21
+
tag:"my-component",
22
+
view:`
23
+
<p>Name changed: {{nameChanged}}</p>
24
+
<p>Name: <input value:bind="name"/></p>
25
+
`,
26
+
ViewModel: {
27
+
nameChanged: {type:"number", default:0},
28
+
name:"string",
29
+
connectedCallback( element ) {
30
+
this.listenTo( "name", function() {
31
+
this.nameChanged++;
32
+
} );
33
+
constdisconnectedCallback=this.stopListening.bind( this );
34
+
return disconnectedCallback;
35
+
}
36
+
}
37
+
});
38
+
</script>
39
+
```
40
+
@highlight 15-21
41
+
@codepen
42
+
43
+
`connectedCallback` is named as such to match the [web components](https://developers.google.com/web/fundamentals/web-components/customelements#reactions) spec for the same concept.
44
+
45
+
@return {Function|undefined} The `disconnectedCallback` function to be called during teardown. Defined in the same closure scope as setup, it's used to tear down anything that was set up during the `connectedCallback` lifecycle hook. If `undefined` is returned, the default `disconnectedCallback` function will be the
46
+
`viewModel`'s [can-event-queue/map/map.stopListening] function. So if you overwrite `disconnectedCallback`,
47
+
you probably want to make sure [can-event-queue/map/map.stopListening] is called.
48
+
49
+
@body
50
+
51
+
## Use
52
+
53
+
Checkout the [guides/recipes/video-player] for a good example of using `connectedCallback` to create
54
+
side effects. For example, it listens to the `viewModel`'s `playing` and `currentTime` and calls
55
+
side-effectual DOM methods like `.play()`.
13
56
14
57
```js
15
-
constPerson=DefineMap.extend( {
16
-
nameChanged:"number",
17
-
name:"string",
18
-
connectedCallback() {
19
-
this.listenTo( "name", function() {
20
-
this.nameChanged++;
21
-
} );
22
-
constdisconnectedCallback=this.stopListening.bind( this );
`connectedCallback` is named as such to match the [web components](https://developers.google.com/web/fundamentals/web-components/customelements#reactions) spec for the same concept.
29
-
30
-
@return {Function|undefined} The `disconnectedCallback` function to be called during teardown. Defined in the same closure scope as setup, it's used to tear down anything that was set up during the `connectedCallback` lifecycle hook.
75
+
As a reminder, event bindings bound with [can-event-queue/map/map.listenTo] (which is available on [can-define/map/map]) will automatically be torn down when the component is removed from the page.
0 commit comments