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

Change user constructor to factoryImpl as an aid to compilation tool... #1437

Merged
merged 1 commit into from
Apr 22, 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
6 changes: 3 additions & 3 deletions PRIMER.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Below is a description of the current Polymer features, followed by individual f
|---------|-------
| [Custom element constructor](#element-constructor) | Polymer.Class({ … });
| [Custom element registration](#register-element) | Polymer({ is: ‘...’, … }};
| [Bespoke constructor support](#bespoke-constructor) | constructor: function() { … }
| [Bespoke constructor support](#bespoke-constructor) | factoryImpl: function() { … }
| [Basic lifecycle callbacks](#basic-callbacks) | created, attached, detached, attributeChanged
| [Native HTML element extension](#type-extension) | extends: ‘…’
| [Configure properties](#property-config) | properties: { … }
Expand Down Expand Up @@ -133,7 +133,7 @@ var el2 = document.createElement('my-element');
<a name="bespoke-constructor"></a>
## Bespoke constructor support

While the standard `Polymer.Class()` and `Polymer()` functions return a basic constructor that can be used to instance the custom element, Polymer also supports providing a bespoke `constructor` function on the prototype that can, for example, accept arguments to configure the element. In this case, the actual constructor returned from `Polymer` will first create an instance using `document.createElement`, then invoke the user-supplied `constructor` function with `this` bound to the element instance.
While the standard `Polymer.Class()` and `Polymer()` functions return a basic constructor that can be used to instance the custom element, Polymer also supports providing a `factoryImpl` function on the prototype that can, for example, accept arguments to configure the element. In this case, the actual constructor returned from `Polymer` will first create an instance using `document.createElement`, then invoke the user-supplied `factoryImpl` function with `this` bound to the element instance.

Example:

Expand All @@ -142,7 +142,7 @@ MyElement = Polymer({

is: 'my-element',

constructor: function(foo, bar) {
factoryImpl: function(foo, bar) {
el.foo = foo;
el.configureWithBar(bar);
},
Expand Down
8 changes: 2 additions & 6 deletions src/micro/constructor.html
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,6 @@
// registration-time

_prepConstructor: function() {
// capture user-supplied `constructor`
if (this.hasOwnProperty('constructor')) {
this._userConstructor = this.constructor;
}
// support both possible `createElement` signatures
this._factoryArgs = this.extends ? [this.extends, this.is] : [this.is];
// thunk the constructor to delegate allocation to `createElement`
Expand All @@ -63,8 +59,8 @@

_factory: function(args) {
var elt = document.createElement.apply(document, this._factoryArgs);
if (this._userConstructor) {
this._userConstructor.apply(elt, args);
if (this.factoryImpl) {
this.factoryImpl.apply(elt, args);
}
return elt;
}
Expand Down
2 changes: 1 addition & 1 deletion test/unit/micro.html
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
test('custom constructor', function() {
var MyElement2 = Polymer({
is: 'my-element2',
constructor: function(title){
factoryImpl: function(title){
this.title = title;
}
});
Expand Down