序列化未发布的 Polymer 属性
Serialize unpublished Polymer attributes
如何使未发布的 Polymer 属性的行为与已发布的属性相似?
未发布的对象属性:
<dynamic-attributes obj="[object Object]"></dynamic-attributes>
已发布的对象属性:
<dynamic-attributes obj="{{ {'hello':'world'} }}"></dynamic-attributes>
诀窍是在 Polymer 获取属性之前获取属性。这可以在 created
生命周期回调中完成。
可以和injectBoundHTML
结合使用渲染
<polymer-element>
<script>
Polymer({
created: function () {
var name = 'seriously-effect-' + this.type;
// get mustache bindings before they get parsed and bound
var instanceDecl = '<' + name + ' ';
var attrs = this.attributes;
for(var i = 0, l = attrs.length, attr; attr = attrs[i], i < l; ++i){
instanceDecl += attr.name + '="' + attr.value + '" ';
}
instanceDecl += '></' + name + '>';
//then parse attributes somehow... and stamp the template
this.templateInstance.model.injectBoundHTML(instanceDecl, parsedAttributesModel);
},
});
</script>
</polymer-element>
如何使未发布的 Polymer 属性的行为与已发布的属性相似?
未发布的对象属性:
<dynamic-attributes obj="[object Object]"></dynamic-attributes>
已发布的对象属性:
<dynamic-attributes obj="{{ {'hello':'world'} }}"></dynamic-attributes>
诀窍是在 Polymer 获取属性之前获取属性。这可以在 created
生命周期回调中完成。
可以和injectBoundHTML
结合使用渲染
<polymer-element>
<script>
Polymer({
created: function () {
var name = 'seriously-effect-' + this.type;
// get mustache bindings before they get parsed and bound
var instanceDecl = '<' + name + ' ';
var attrs = this.attributes;
for(var i = 0, l = attrs.length, attr; attr = attrs[i], i < l; ++i){
instanceDecl += attr.name + '="' + attr.value + '" ';
}
instanceDecl += '></' + name + '>';
//then parse attributes somehow... and stamp the template
this.templateInstance.model.injectBoundHTML(instanceDecl, parsedAttributesModel);
},
});
</script>
</polymer-element>