From Zero to Polymer: Step 03 - HTML import
This file contains 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> | |
<link rel="import" href="my-element.html"> | |
</head> | |
<body> | |
<my-element></my-element> | |
<my-element></my-element> | |
</body> | |
</html> |
This file contains 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
<!-- my-element.html --> | |
<template id="my-element-template"> | |
<p>My <b>custom element</b> markup!</p> | |
</template> | |
<script> | |
// thisDoc refers to the "importee", which is my-element.html | |
var thisDoc = document.currentScript.ownerDocument; | |
// thatDoc refers to the "importer", which is index.html | |
var thatDoc = document; | |
var MyElementProto = Object.create(HTMLElement.prototype); | |
MyElementProto.createdCallback = function() { | |
var t = thisDoc.querySelector('#my-element-template'); | |
var clone = thisDoc.importNode(t.content, true); | |
this.appendChild(clone); | |
}; | |
var MyElement = thatDoc.registerElement( | |
'my-element', { prototype: MyElementProto } | |
); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment