Created
April 19, 2015 21:29
-
-
Save kennethlynne/69f36ce75c9351a00b43 to your computer and use it in GitHub Desktop.
Web component demo
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
<!-- In index.html: <link rel="import" href="components/list-item.html"> --> | |
<template> | |
<style> | |
</style> | |
<div>Web component, right there</div> | |
</template> | |
<script> | |
(function (window, document) { | |
// importer = index.html | |
var importer = document; | |
// Refers to the "importee", which is src/list-item.html | |
var thisDoc = (importer._currentScript || importer.currentScript).ownerDocument; | |
// Gets content from <template> | |
var template = thisDoc.querySelector('template').content; | |
// Creates an object based in the HTML Element prototype | |
var ListItemElementPrototype = Object.create(HTMLElement.prototype); | |
// Fires when an instance of the element is created | |
ListItemElementPrototype.createdCallback = function () { | |
// Creates the shadow root | |
var shadowRoot = this.createShadowRoot(); | |
// Adds a template clone into shadow root | |
var clone = importer.importNode(template, true); | |
shadowRoot.appendChild(clone); | |
}; | |
// Registers <list-item> in the main document | |
window.MyElement = importer.registerElement('list-item', { | |
prototype: ListItemElementPrototype | |
}); | |
})(window, document); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment