factoryImpl属性用于在创建新对象时,为新对象初始化参数。如不添加该属性时,注册对象的动作:
// register an element
MyElement = Polymer({
is: 'my-element',
// See below for lifecycle callbacks
created: function() {
this.textContent = 'My element!';
}
});
// create an instance with createElement:
var el1 = document.createElement('my-element');
// ... or with the constructor:
var el2 = new MyElement();
添加了factoryImpl后,可以这样:
MyElement = Polymer({
is: 'my-element',
factoryImpl: function(foo, bar) {
this.foo = foo;
this.configureWithBar(bar);
},
configureWithBar: function(bar) {
...
}
});
var el = new MyElement(42, 'octopus');
使用factoryImpl的注意事项:
-
The
factoryImpl
method is only invoked when you create an element using the constructor. ThefactoryImpl
method is not called if the element is created from markup by the HTML parser, or if the element is created usingdocument.createElement
. -
The
factoryImpl
method is called after the element is initialized (local DOM created, default values set, and so on). See Ready callback and element initialization for more information.