Skip to content
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

use hasOwnProperty to avoid overwriting prototype methods when mixing in behaviors (+test) #1473

Merged
merged 4 commits into from
Apr 30, 2015
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
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