Skip to content

add behaviors to instances too #4397

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

Merged
merged 5 commits into from
Mar 9, 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
7 changes: 3 additions & 4 deletions lib/legacy/class.html
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,16 @@
if (!Array.isArray(behaviors)) {
behaviors = [behaviors];
}
let superBehaviors = klass.behaviors;
let superBehaviors = klass.prototype.behaviors;
// get flattened, deduped list of behaviors *not* already on super class
behaviors = flattenBehaviors(behaviors, null, superBehaviors);
// mixin new behaviors
klass = _mixinBehaviors(behaviors, klass);
if (superBehaviors) {
behaviors = superBehaviors.concat(behaviors);
}
klass.behaviors = behaviors;
// Set behaviors on prototype for BC...
klass.prototype.behaviors = behaviors;
return klass;
}

Expand Down Expand Up @@ -320,8 +321,6 @@
LegacyElementMixin(HTMLElement));
// decorate klass with registration info
klass.is = info.is;
// behaviors on prototype for BC...
klass.prototype.behaviors = klass.behaviors;
// NOTE: while we could call `beforeRegister` here to maintain
// some BC, the state of the element at this point is not as it was in 1.0
// In 1.0, the method was called *after* mixing prototypes together
Expand Down
18 changes: 13 additions & 5 deletions test/unit/mixin-behaviors.html
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@
this._createMethodObserver('propChanged2(prop)');
this.registeredCount++;
this.registeredProps = [this.prop1, this.prop2, this.prop3];
this.registeredBehaviors = this.constructor.behaviors;
this.registeredBehaviors = this.behaviors;
},
prop1: true,
ready: function() {
Expand Down Expand Up @@ -382,6 +382,10 @@
assert.equal(el.__label, 'foo');
});

test('instance behaviors', function() {
assert.equal(el.behaviors.length, 1);
});

test('listener from behavior', function() {
el.fire('change', {value: 'bar'});
assert.equal(el.__change, 'bar');
Expand Down Expand Up @@ -413,7 +417,7 @@
var el = fixture('registered');
assert.equal(el.registeredCount, 4);
assert.equal(el.registeredBehaviors.length, 3);
assert.equal(el.registeredBehaviors, el.constructor.behaviors);
assert.equal(el.registeredBehaviors, el.behaviors);
assert.deepEqual(el.registeredProps, [true, true, true]);
});

Expand Down Expand Up @@ -447,6 +451,10 @@
assert.equal(el.__readyB, true);
});

test('instance behaviors', function() {
assert.equal(el.behaviors.length, 2);
});

test('properties from behaviors', function() {
el.label = 'foo';
assert.equal(el.__label, 'foo');
Expand Down Expand Up @@ -510,13 +518,13 @@
test('behavior array is unique', function() {
customElements.define('behavior-unique', Polymer.mixinBehaviors(
[Polymer.BehaviorA, Polymer.BehaviorA], Polymer.Element));
assert.equal(document.createElement('behavior-unique').constructor.behaviors.length, 1);
assert.equal(document.createElement('behavior-unique').behaviors.length, 1);
});

test('duplicate behaviors keep first behavior', function() {
customElements.define('behavior-unique-last-behavior', Polymer.mixinBehaviors(
[Polymer.BehaviorA, Polymer.BehaviorB, Polymer.BehaviorC, Polymer.BehaviorA, Polymer.BehaviorB], Polymer.Element));
var behaviors = document.createElement('behavior-unique-last-behavior').constructor.behaviors;
var behaviors = document.createElement('behavior-unique-last-behavior').behaviors;
assert.deepEqual(behaviors, [Polymer.BehaviorC, Polymer.BehaviorA, Polymer.BehaviorB]);
});

Expand All @@ -530,7 +538,7 @@
});

test('nested-behavior dedups', function() {
assert.equal(el.constructor.behaviors.length, 4);
assert.equal(el.behaviors.length, 4);
});

test('nested-behavior overrides ordering', function() {
Expand Down