Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUGFIX release] this.$() undefined in willDestroyElement hook #14685

Merged
merged 2 commits into from
Dec 14, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { strip } from '../../utils/abstract-test-case';
import { moduleFor, RenderingTest } from '../../utils/test-case';
import { getViewId, getViewElement } from 'ember-views';
import { classes } from '../../utils/test-helpers';
import { tryInvoke } from 'ember-utils';
import { runAppend } from 'internal-test-helpers';

class LifeCycleHooksTest extends RenderingTest {
constructor() {
Expand Down Expand Up @@ -1568,6 +1570,36 @@ moduleFor('Run loop and lifecycle hooks', class extends RenderingTest {
}
]);
}
['@test lifecycle hooks have proper access to this.$()'](assert) {
assert.expect(6);
let component;
let FooBarComponent = Component.extend({
tagName: 'div',
init() {
assert.notOk(this.$(), 'no access to element via this.$() on init() enter');
this._super(...arguments);
assert.notOk(this.$(), 'no access to element via this.$() after init() finished');
},
willInsertElement() {
component = this;
assert.ok(this.$(), 'willInsertElement has access to element via this.$()');
},
didInsertElement() {
assert.ok(this.$(), 'didInsertElement has access to element via this.$()');
},
willDestroyElement() {
assert.ok(this.$(), 'willDestroyElement has access to element via this.$()');
},
didDestroyElement() {
assert.notOk(this.$(), 'didDestroyElement does not have access to element via this.$()');
}
});
this.registerComponent('foo-bar', { ComponentClass: FooBarComponent, template: 'hello' });
let { owner } = this;
let comp = owner.lookup('component:foo-bar');
runAppend(comp);
this.runTask(() => tryInvoke(component, 'destroy'));
}
});

function assertDestroyHooks(assert, _actual, _expected) {
Expand Down
4 changes: 3 additions & 1 deletion packages/ember-views/lib/mixins/view_support.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,9 @@ export default Mixin.create({
*/
$(sel) {
assert('You cannot access this.$() on a component with `tagName: \'\'` specified.', this.tagName !== '');
return this._currentState.$(this, sel);
if (this.element) {
return sel ? jQuery(sel, this.element) : jQuery(this.element);
}
},

/**
Expand Down
4 changes: 0 additions & 4 deletions packages/ember-views/lib/views/states/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@ export default {
throw new EmberError('You can\'t use appendChild outside of the rendering process');
},

$() {
return undefined;
},

// Handle events from `Ember.EventDispatcher`
handleEvent() {
return true; // continue event propagation
Expand Down
5 changes: 0 additions & 5 deletions packages/ember-views/lib/views/states/has_element.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
import { assign } from 'ember-utils';
import _default from './default';
import { run, flaggedInstrument } from 'ember-metal';
import jQuery from '../../system/jquery';

const hasElement = Object.create(_default);

assign(hasElement, {
$(view, sel) {
let elem = view.element;
return sel ? jQuery(sel, elem) : jQuery(elem);
},

rerender(view) {
view.renderer.rerender(view);
Expand Down