Skip to content

Commit

Permalink
Merge pull request #94 from PolymerLabs/fix-92
Browse files Browse the repository at this point in the history
Fixes #92. Array behaviors now properly add to rather than replace ex…
  • Loading branch information
kevinpschaaf authored Aug 25, 2016
2 parents 24881c4 + c1002f4 commit a4f8f58
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 8 deletions.
12 changes: 6 additions & 6 deletions src/compat/class.html
Original file line number Diff line number Diff line change
Expand Up @@ -45,25 +45,25 @@
return klass;
}

var flattenBehaviors = function(behaviors) {
var r = [];
var flattenBehaviors = function(behaviors, list) {
list = list || [];
if (behaviors) {
for (var i=0; i < behaviors.length; i++) {
var b = behaviors[i];
if (b) {
if (Array.isArray(b)) {
r = flattenBehaviors(b);
flattenBehaviors(b, list);
} else {
if (r.indexOf(b) < 0) {
r.push(b);
if (list.indexOf(b) < 0) {
list.push(b);
}
}
} else {
Polymer._warn('behavior is null, check for missing or 404 import');
}
}
}
return r;
return list;
}

var MixinBehavior = function(behavior, Base) {
Expand Down
66 changes: 66 additions & 0 deletions test/smoke/behaviors-compose.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<!doctype html>
<!--
@license
Copyright (c) 2014 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
-->
<html>
<head>
<title>Polymer - composed behaviors</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="../../../webcomponentsjs/webcomponents-lite.js"></script>

<link rel="import" href="../../polymer.html">

</head>
<body>
<script>
var SomeBehavior = [{
properties: {
some: {
type: Boolean,
value: true
}
}
}, {
properties: {
someExtended: {
type: Boolean,
value: true
}
}
}];

var OtherBehavior = [{
properties: {
other: {
type: Boolean,
value: true
}
}
}, {
properties: {
otherExtended: {
type: Boolean,
value: true
}
}
}];

Polymer({
is: 'x-sample',
behaviors: [SomeBehavior, OtherBehavior],
attached: function() {
console.log(this.some, this.someExtended, this.other, this.otherExtended);
}
});
</script>

<x-sample></x-sample>
</body>
</html>
8 changes: 6 additions & 2 deletions test/unit/behaviors.html
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,8 @@
is: 'nested-behaviors',

behaviors: [
[[Polymer.BehaviorC, Polymer.BehaviorB], Polymer.BehaviorA],
Polymer.BehaviorD
[Polymer.BehaviorB, [Polymer.BehaviorC, Polymer.BehaviorB], Polymer.BehaviorA],
[Polymer.BehaviorD]
]
});
</script>
Expand Down Expand Up @@ -458,6 +458,10 @@
el = fixture('nested');
});

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

test('nested-behavior overrides ordering', function() {
assert.ok(el.hasBehaviorA, "missing BehaviorA");
assert.ok(el.hasBehaviorB, "missing BehaviorB");
Expand Down

0 comments on commit a4f8f58

Please sign in to comment.