Skip to content

Commit

Permalink
Merge pull request #1473 from Polymer/0.8-behaviors-first
Browse files Browse the repository at this point in the history
use `hasOwnProperty` to avoid overwriting prototype methods when mixing in behaviors (+test)
  • Loading branch information
Steve Orvell committed Apr 30, 2015
2 parents db6ebd5 + 260dfdd commit bcd2923
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 15 deletions.
26 changes: 16 additions & 10 deletions src/micro/behaviors.html
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,13 @@
behaviors: [],

_prepBehaviors: function() {
this._flattenBehaviors();
this._prepBehavior(this);
this.behaviors.forEach(function(b) {
this._mixinBehavior(b);
this._prepBehavior(b);
}, this);
this.behaviors = this._flattenBehaviorsList(this.behaviors);
this._prepAllBehaviors();
},

_flattenBehaviors: function() {
_flattenBehaviorsList: function(behaviors) {
var flat = [];
this.behaviors.forEach(function(b) {
behaviors.forEach(function(b) {
if (!b) {
console.warn('Polymer: undefined behavior in [' + this.is + ']');
} else if (b instanceof Array) {
Expand All @@ -62,7 +58,15 @@
flat.push(b);
}
}, this);
this.behaviors = flat;
return flat;
},

_prepAllBehaviors: function() {
this.behaviors.forEach(function(b) {
this._mixinBehavior(b);
this._prepBehavior(b);
}, this);
this._prepBehavior(this);
},

_mixinBehavior: function(b) {
Expand All @@ -82,7 +86,9 @@
case 'ready':
break;
default:
this.copyOwnProperty(n, b, this);
if (!this.hasOwnProperty(n)) {
this.copyOwnProperty(n, b, this);
}
break;
}
}, this);
Expand Down
4 changes: 2 additions & 2 deletions test/runner.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
'unit/micro.html',
'unit/attributes.html',
'unit/async.html',
'unit/behaviors.html',
'unit/template.html',
'unit/ready.html',
'unit/ready-shadow.html',
Expand All @@ -40,8 +41,7 @@
'unit/x-style.html',
'unit/dynamic-import.html',
'unit/x-repeat.html',
'unit/x-if.html',
'unit/behaviors.html'
'unit/x-if.html'
]);
</script>
</body>
Expand Down
15 changes: 12 additions & 3 deletions test/unit/behaviors-elements.html
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@

_changeHandler: function(e) {
this.__change = e.detail.value;
},

_toOverride: function() {
}

};
Expand Down Expand Up @@ -71,9 +74,11 @@

ready: function() {
this.__readyB = true;
}
},

_toOverride: function() {}

}
};

</script>

Expand All @@ -97,6 +102,8 @@

<script>

Polymer._toOverride = function() {};

Polymer({

behaviors: [
Expand All @@ -117,7 +124,9 @@

_fooChanged: function(foo) {
this.__foo = foo;
}
},

_toOverride: Polymer._toOverride

});

Expand Down
3 changes: 3 additions & 0 deletions test/unit/behaviors.html
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@
assert.equal(typeof el._setHasOptionsB, 'function');
});

test('behavior overrides are last', function() {
assert.equal(el._toOverride, Polymer._toOverride);
});
});

</script>
Expand Down

0 comments on commit bcd2923

Please sign in to comment.