Skip to content

Commit

Permalink
Merge pull request #1437 from Polymer/0.8-factory
Browse files Browse the repository at this point in the history
Change user `constructor` to `factoryImpl` as an aid to compilation tool...
  • Loading branch information
kevinpschaaf committed Apr 22, 2015
2 parents bd3af28 + d9d7021 commit 4a7c8c6
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 10 deletions.
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

0 comments on commit 4a7c8c6

Please sign in to comment.