Skip to content

Commit

Permalink
[BUGFIX beta] Ensure that this._super is called by Components.
Browse files Browse the repository at this point in the history
When implementing `init` in a component subclass you must call
`this._super(...arguments)`.  If you do not you will get an error.
Unfortunately, that error is **very** hard to understand and is
completely unrelated to code that you have written.

This change adds a small flag (via symbol to avoid exposing this
publicly) that we can use to ensure that `_super` was called
properly.

---

After these changes, given the following:

```javascript
import Ember from 'ember';

export default Ember.Component.extend({
  init() {
    this.doThings();
  }
});
```

Will throw an error that explains that you must call `_super`:

```
You must call `this._super(...arguments);` when implementing `init`
in a component. Please update ${this} to call `this._super` from `init`.
```
  • Loading branch information
rwjblue committed Sep 26, 2015
1 parent ed0bd63 commit bf455fb
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
10 changes: 10 additions & 0 deletions packages/ember-views/lib/mixins/view_support.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import { guidFor } from 'ember-metal/utils';
import { computed } from 'ember-metal/computed';
import { Mixin } from 'ember-metal/mixin';
import { POST_INIT } from 'ember-runtime/system/core_object';
import { symbol } from 'ember-metal/utils';

const INIT_WAS_CALLED = symbol('INIT_WAS_CALLED');

import jQuery from 'ember-views/system/jquery';

Expand Down Expand Up @@ -610,6 +613,7 @@ export default Mixin.create({
this.scheduledRevalidation = false;

this._super(...arguments);
this[INIT_WAS_CALLED] = true;

assert(
'Using a custom `.render` function is no longer supported.',
Expand All @@ -628,6 +632,12 @@ export default Mixin.create({
*/
[POST_INIT]: function() {
this._super(...arguments);

assert(
`You must call \`this._super(...arguments);\` when implementing \`init\` in a component. Please update ${this} to call \`this._super\` from \`init\`.`,
this[INIT_WAS_CALLED]
);

this.renderer.componentInitAttrs(this, this.attrs || {});
},

Expand Down
10 changes: 10 additions & 0 deletions packages/ember-views/tests/views/component_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ QUnit.module('Ember.Component', {
}
});

QUnit.test('throws an error if `this._super` is not called from `init`', function() {
let TestComponent = Component.extend({
init() { }
});

expectAssertion(function() {
TestComponent.create();
}, /You must call `this._super\(...arguments\);` when implementing `init` in a component. Please update .* to call `this._super` from `init`/);
});

QUnit.test('can access `actions` hash via `_actions` [DEPRECATED]', function() {
expect(2);

Expand Down

0 comments on commit bf455fb

Please sign in to comment.