Skip to content

Commit

Permalink
Merge pull request #4397 from Polymer/fix-4396
Browse files Browse the repository at this point in the history
add `behaviors` to instances too
  • Loading branch information
Steve Orvell authored Mar 9, 2017
2 parents 4ae65ba + bb52071 commit a158a1f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
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

0 comments on commit a158a1f

Please sign in to comment.