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

Ensure _registered is called 1x for each element class using LegacyElementMixin #4438

Merged
merged 1 commit into from
Mar 17, 2017
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
12 changes: 3 additions & 9 deletions lib/legacy/class.html
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,6 @@

function GenerateClassFromInfo(info, Base) {

let registered = false;

class PolymerGenerated extends Base {

static get properties() {
Expand Down Expand Up @@ -163,13 +161,9 @@
}

_registered() {
if (!registered) {
super._registered();
// call `registered` only if it was not called for *this* constructor
registered = true;
if (info.registered) {
info.registered.call(Object.getPrototypeOf(this));
}
super._registered();
if (info.registered) {
info.registered.call(Object.getPrototypeOf(this));
}
}

Expand Down
9 changes: 7 additions & 2 deletions lib/legacy/legacy-element-mixin.html
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,17 @@

/**
* Overrides the default `Polymer.PropertyEffects` implementation to
* add support for one-time `registration` callback.
* add support for class initialization via the `_registered` callback.
* This is called only when the first instance of the element is created.
*
* @override
*/
_initializeProperties() {
this._registered();
let proto = Object.getPrototypeOf(this);
if (!proto.hasOwnProperty('__hasRegisterFinished')) {
proto.__hasRegisterFinished = true;
this._registered();
}
super._initializeProperties();
}

Expand Down
24 changes: 22 additions & 2 deletions test/unit/mixin-behaviors.html
Original file line number Diff line number Diff line change
Expand Up @@ -329,12 +329,18 @@
BehaviorRegistered.prototype.registeredCount = 0;

customElements.define(BehaviorRegistered.is, BehaviorRegistered);

class BehaviorRegisteredExt extends BehaviorRegistered {
static get is() { return 'behavior-registered-ext'}
}

BehaviorRegisteredExt.prototype.registeredCount = 0;

customElements.define(BehaviorRegisteredExt.is, BehaviorRegisteredExt);
});
</script>
</dom-module>

</script>

<test-fixture id="single">
<template>
<single-behavior></single-behavior>
Expand Down Expand Up @@ -364,6 +370,12 @@
<behavior-registered></behavior-registered>
</template>
</test-fixture>

<test-fixture id="registered-ext">
<template>
<behavior-registered-ext></behavior-registered-ext>
</template>
</test-fixture>
<script>

suite('single behavior element', function() {
Expand Down Expand Up @@ -421,6 +433,14 @@
assert.deepEqual(el.registeredProps, [true, true, true]);
});

test('extending element with behaviors with registered properly registers', function() {
var el = fixture('registered-ext');
assert.equal(el.registeredCount, 4);
assert.equal(el.registeredBehaviors.length, 3);
assert.equal(el.registeredBehaviors, el.behaviors);
assert.deepEqual(el.registeredProps, [true, true, true]);
});

});

suite('behavior lifecycle', function() {
Expand Down