Skip to content

Commit

Permalink
Fixes #4437. Ensure _registered is called 1x for each element class…
Browse files Browse the repository at this point in the history
… using `LegacyElementMixin`. Ensure that a behaviors’s `registered` method is called for any extending class.
  • Loading branch information
Steven Orvell committed Mar 16, 2017
1 parent 04c5053 commit de09d73
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 13 deletions.
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

0 comments on commit de09d73

Please sign in to comment.