Created
September 27, 2013 21:14
-
-
Save dfreedm/6735285 to your computer and use it in GitHub Desktop.
Template Subtype Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!DOCTYPE html> | |
| <html lang="en"> | |
| <head> | |
| <meta charset="UTF-8"> | |
| <title>Template Subtypes</title> | |
| <script src="polymer/CustomElements/custom-elements.js"></script> | |
| <script src="polymer/TemplateBinding/load.js"></script> | |
| </head> | |
| <body> | |
| <script> | |
| function makeCustom(tagName, className, superTag, superClass) { | |
| var creationClass = window[superClass] || HTMLElement; | |
| var cls = Object.create(creationClass.prototype); | |
| cls.createdCallback = function(){ this.textContent = tagName }; | |
| var attribs = { | |
| prototype: cls | |
| }; | |
| if (superTag) { | |
| attribs.extends = superTag; | |
| } | |
| window[className] = document.register(tagName, attribs); | |
| } | |
| makeCustom('my-element', 'MyElement'); | |
| try { | |
| makeCustom('my-element-foo', 'MyElementFoo', 'my-element', 'MyElement'); | |
| } catch(e) { | |
| console.error(e.stack || e); | |
| } | |
| makeCustom('div-extending-foo', 'ExtendDiv', 'div', 'HTMLDivElement'); | |
| </script> | |
| <!-- template --> | |
| <template id="t" bind> | |
| <!-- div is custom element --> | |
| <div is="div-extending-{{type}}">div</div> | |
| <!-- custom element is other custom element --> | |
| <my-element is="my-element-{{type}}"></my-element> | |
| </template> | |
| <script> | |
| t = document.querySelector('#t'); | |
| // if is=type can be set, output on page should be the custom element's name | |
| t.model = {type: 'foo'}; | |
| </script> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment