Polymer: Create A "general" Custom Element
Solution 1:
It’s hard to say what you are trying to achieve, but I would point at some glitches within your code and provide the working version with paper-button
instead of flip-card
(since I have no code of flip-card
.)
TL;DR Live preview: http://plnkr.co/edit/yO865EkclZAsWBhzVSU4?p=preview
The main issue with your code is that you call function
addElementToDOM
instead of passing it as a callback as requested byPolymer.
.function() { addElementToDOM(...); }
would be OK.You use hyphen-less names for custom elements (like
front
andback
) which is conventionally prohibited. Please use dashed as in my example.You have your function polluted into global space for no reason. I put it inside an element.
You were likely want to puts your custom
flip-card
betweenfront
andback
, but there is no vestige ofcontent select='front/back'
tag inside your template.You are trying to access
shadowRoot
in improper way. You might want to take a look atgetDistributedNodes
function, but in this case it’s easier to use naming convention and address an element simply bythis.$.elementHere
.You are supposed to explicitly specify
<body unresolved>
for the polyfill to take care about unresolved things.
The working code:
<!doctype html><html><head><metaname="viewport"content="width=device-width, initial-scale=1, user-scalable=no"><title>Add paper button</title><scriptsrc="https://www.polymer-project.org/components/webcomponentsjs/webcomponents.js"></script><linkhref="https://www.polymer-project.org/components/polymer/polymer.html" ></head><bodyunresolved><polymer-elementname="general-element"attributes="name address"><template><contentselect='my-front'></content><divid="elementHere"></div><contentselect='my-back'></content></template><script>Polymer ({
addElementToDOM: function ( element_name ) {
var elem = this.$.elementHere;
add_elem = document.createElement( element_name );
add_elem.appendChild(document.createTextNode('¡Hola!'));
elem.appendChild( add_elem );
},
domReady: function() {
if ( this.address && this.name ) {
var self = this;
Polymer.(
[self.address],
function() { self.addElementToDOM( self.name ); }
);
}
}
});
</script></polymer-element><general-elementname="paper-button"address="https://www.polymer-project.org/components/paper-button/paper-button.html"><my-front>
FRONT
</my-front><my-back>
BACK
</my-back></general-element></body></html>
I bet, your code could be working by applying the fix #1 but I strongly suggest you to fix other glitches as well. Hope it helps.
P.S. Why on the earth you needed to dynamically your element? What’s wrong with simple
<template><contentselect='my-front'></content><!-- ⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓ --><flip-card></flip-card><contentselect='my-back'></content></template>
Post a Comment for "Polymer: Create A "general" Custom Element"