Skip to content
This repository was archived by the owner on May 26, 2019. It is now read-only.

Commit 6123846

Browse files
committed
Remove this.$ references from component guide
1 parent 46de7c5 commit 6123846

File tree

1 file changed

+36
-15
lines changed

1 file changed

+36
-15
lines changed

source/components/the-component-lifecycle.md

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -123,39 +123,54 @@ Ember guarantees that, by the time `didInsertElement()` is called:
123123
1. The component's element has been both created and inserted into the
124124
DOM.
125125
2. The component's element is accessible via the component's
126-
[`$()`][dollar]
127-
method.
126+
[`element`][element]
127+
property.
128128

129-
A component's [`$()`][dollar] method allows you to access the component's DOM element by returning a JQuery element.
130-
For example, you can set an attribute using jQuery's `attr()` method:
129+
A component's [`element`][element] property allows you to access the component's DOM element.
130+
For example, you can set a property:
131131

132132
```app/components/profile-editor.js
133133
import Component from '@ember/component';
134134

135135
export default Component.extend({
136136
didInsertElement() {
137137
this._super(...arguments);
138-
this.$().attr('contenteditable', true);
138+
this.element.contentEditable = 'true';
139139
}
140140
});
141141
```
142142

143-
[`$()`][dollar] will, by default, return a jQuery object for the component's root element, but you can also target child elements within the component's template by passing a selector:
143+
[`element`][element] will return the component's root element,
144+
but you can also target child elements within the component's template by using `querySelector`:
144145

145146
```app/components/profile-editor.js
146147
import Component from '@ember/component';
147148

148149
export default Component.extend({
149150
didInsertElement() {
150151
this._super(...arguments);
151-
this.$('div p button').addClass('enabled');
152+
this.element.querySelector('div p button').classList.add('enabled');
152153
}
153154
});
154155
```
155156

156157
Let's initialize our date picker by overriding the [`didInsertElement()`][did-insert-element] method.
157158

158-
Date picker libraries usually attach to an `<input>` element, so we will use jQuery to find an appropriate input within our component's template.
159+
Date picker libraries usually attach to an `<input>` element, so we will look for an appropriate input within our component's template.
160+
161+
```app/components/profile-editor.js
162+
import Component from '@ember/component';
163+
164+
export default Component.extend({
165+
didInsertElement() {
166+
this._super(...arguments);
167+
myDatePickerLib(this.element.querySelector('input.date'));
168+
}
169+
});
170+
```
171+
172+
Should our data picker library come as a jQuery plugin,
173+
we can also use the component's [`$`](dollar) method to get the component's root element as a jQuery object:
159174

160175
```app/components/profile-editor.js
161176
import Component from '@ember/component';
@@ -168,6 +183,10 @@ export default Component.extend({
168183
});
169184
```
170185

186+
Note however that [`$`](dollar) should not be used if you do not want to depend on jQuery,
187+
as this code would break when [removing jQuery](../addons-and-dependencies/removing-jquery).
188+
189+
171190
[`didInsertElement()`][did-insert-element] is also a good place to
172191
attach event listeners. This is particularly useful for custom events or
173192
other browser events which do not have a [built-in event
@@ -182,9 +201,10 @@ import Component from '@ember/component';
182201
export default Component.extend({
183202
didInsertElement() {
184203
this._super(...arguments);
185-
this.$().on('animationend', () => {
186-
$(this).removeClass('sliding-anim');
187-
});
204+
this._eventListener = () => {
205+
this.element.classList.remove('sliding-anim');
206+
};
207+
this.element.addEventListener('animationend', this._eventListener);
188208
}
189209
});
190210
```
@@ -199,7 +219,8 @@ There are a few things to note about the `didInsertElement()` hook:
199219
particularly when order of execution is important.
200220

201221
[did-insert-element]: https://www.emberjs.com/api/ember/2.16/classes/Component/events/didInsertElement?anchor=didInsertElement
202-
[dollar]: https://www.emberjs.com/api/ember/2.16/classes/Component/methods/$?anchor=%24
222+
[element]: https://www.emberjs.com/api/ember/release/classes/Component/properties/element?anchor=element
223+
[dollar]: https://www.emberjs.com/api/ember/release/classes/Component/methods/$?anchor=%24
203224
[event-names]: http://guides.emberjs.com/v2.1.0/components/handling-events/#toc_event-names
204225

205226
### Making Updates to the Rendered DOM with `didRender`
@@ -247,7 +268,7 @@ export default Component.extend({
247268

248269
didRender() {
249270
this._super(...arguments);
250-
this.$('.item-list').scrollTop(this.$('.selected-item').position.top);
271+
this.element.querySelector('.item-list').scrollTop = this.element.querySelector('.selected-item').offsetTop;
251272
}
252273
});
253274
```
@@ -273,8 +294,8 @@ import Component from '@ember/component';
273294

274295
export default Component.extend({
275296
willDestroyElement() {
276-
this.$().off('animationend');
277-
this.$('input.date').myDatepickerLib().destroy();
297+
this.element.removeEventListener('animationend', this._eventListener);
298+
myDatepickerLib.destroy(this.element.querySelector('input.date'));
278299
this._super(...arguments);
279300
}
280301
});

0 commit comments

Comments
 (0)