Skip to content

Commit

Permalink
Fix bespoke constructors, add test, and document. Also fixes #1151.
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinpschaaf committed Mar 9, 2015
1 parent 875a785 commit 24a2914
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/features/micro/constructor.html
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
this._factoryArgs = this.extends ? [this.extends, this.is] : [this.is];
// thunk the constructor to delegate allocation to `createElement`
var ctor = function() {
return this._factory();
return this._factory(arguments);
};
if (this.hasOwnProperty('extends')) {
ctor.extends = this.extends;
Expand All @@ -61,10 +61,10 @@
ctor.prototype = this;
},

_factory: function() {
_factory: function(args) {
var elt = document.createElement.apply(document, this._factoryArgs);
if (this._userConstructor) {
this._userConstructor.apply(elt, arguments);
this._userConstructor.apply(elt, args);
}
return elt;
}
Expand Down
18 changes: 17 additions & 1 deletion test/unit/micro.html
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,23 @@
assert.instanceOf(el, HTMLElement, 'Instance of HTMLInputElement');
});

// TODO(sjmiles): add test for user supplied constructor
test('custom constructor', function() {
var MyElement2 = Polymer({
is: 'my-element2',
constructor: function(title){
this.title = title;
}
});
var el = new MyElement2('my title');
document.body.appendChild(el);
if (Object.__proto__) {
// instanceof Constructor only supported where proto swizzling is possible
assert.instanceOf(el, MyElement2, 'Instance of MyElement');
assert.instanceOf(el, HTMLElement, 'Instance of HTMLElement');
}
assert.equal(el.title, 'my title', 'Argument passed to constructor');
});

});

</script>
Expand Down

0 comments on commit 24a2914

Please sign in to comment.