Skip to content

Commit

Permalink
Avoid copying certain properties from behaviors
Browse files Browse the repository at this point in the history
This better matches what Polymer 1.x did for:
* hostAttributes
* listeners
* properties
* observers
  • Loading branch information
Steven Orvell committed Nov 9, 2018
1 parent b3568bb commit 5b34ce9
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 11 deletions.
33 changes: 22 additions & 11 deletions lib/legacy/class.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,37 @@

'use strict';

const metaProps = {
const lifecycleProps = {
attached: true,
detached: true,
ready: true,
created: true,
beforeRegister: true,
registered: true,
attributeChanged: true,
listeners: true,
hostAttributes: true
};

const excludeProps = Object.assign({
const excludeOnInfo = {
attached: true,
detached: true,
ready: true,
created: true,
beforeRegister: true,
registered: true,
attributeChanged: true,
behaviors: true
}, metaProps);
};

const lifecycleProps = Object.assign({
const excludeOnBehaviors = Object.assign({
listeners: true,
hostAttributes: true
}, metaProps);
hostAttributes: true,
properties: true,
observers: true,
}, excludeOnInfo);

function copyProperties(source, target) {
function copyProperties(source, target, excludeProps) {
for (let p in source) {
// NOTE: cannot copy `excludeProps` methods onto prototype at least because
// `super.ready` must be called and is not included in the user fn.
Expand Down Expand Up @@ -97,12 +108,12 @@
// (again same as 1.x)
function applyBehaviors(proto, behaviors, lifecycle) {
for (let i=0; i<behaviors.length; i++) {
applyInfo(proto, behaviors[i], lifecycle);
applyInfo(proto, behaviors[i], lifecycle, excludeOnBehaviors);
}
}

function applyInfo(proto, info, lifecycle) {
copyProperties(info, proto);
function applyInfo(proto, info, lifecycle, excludeProps) {
copyProperties(info, proto, excludeProps);
for (let p in lifecycleProps) {
if (info[p]) {
lifecycle[p] = lifecycle[p] || [];
Expand Down Expand Up @@ -282,7 +293,7 @@
if (behaviorList) {
applyBehaviors(proto, behaviorList, lifecycle);
}
applyInfo(proto, info, lifecycle);
applyInfo(proto, info, lifecycle, excludeOnInfo);
let list = lifecycle.beforeRegister;
if (list) {
for (let i=0; i < list.length; i++) {
Expand Down
21 changes: 21 additions & 0 deletions test/unit/behaviors.html
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@
}
},

observers: [],

_simpleProperty: 'A',

hostAttributes: {
Expand Down Expand Up @@ -371,6 +373,11 @@
}
});

Polymer({
is: 'behavior-properties',
behaviors: [window.BehaviorA]
});

});
</script>

Expand Down Expand Up @@ -434,6 +441,12 @@
</template>
</test-fixture>

<test-fixture id="behavior-properties">
<template>
<behavior-properties></behavior-properties>
</template>
</test-fixture>

<script>

suite('single behavior element', function() {
Expand Down Expand Up @@ -479,6 +492,14 @@
assert.equal(el.computeA, true);
});

test('special properties not copied from behavior to element', function() {
const el = fixture('behavior-properties');
assert.notOk(el.properties);
assert.notOk(el.observers);
assert.notOk(el.hostAttributes);
assert.notOk(el.listeners);
});

});

suite('behavior.registered', function() {
Expand Down

0 comments on commit 5b34ce9

Please sign in to comment.