On Github petejohanson / nerdsummit-2015-polymer
<head> <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css"> <script src="//code.jquery.com/jquery-1.10.2.js"></script> <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script> <script> $(function() { $( "#menu" ).menu({ disabled: true }); }); </script> </head> <body> <ul id="menu"> <li>Item 1</li> <li>Item 2</li> </ul>
<head> <link rel="import" href="paper-item/paper-item.html"> <link rel="import" href="paper-menu/paper-menu.html"> <head> <body> <paper-menu selected="1"> <paper-item>Item 1</paper-item> <paper-item>Item 2</paper-item> </paper-menu> </body>
Polymer({ is: 'my-gravatar', properties: { email: String, size: { type: String, value: '' }, /* ... */ } });
Polymer({ is: 'my-gravatar', properties: { email: String, size: String, imgsrc: { type: String, computed: 'computeImageSource(email, size)' } }, computeImageSource: function(email, size) { return ...; } });
Needed for two-way data binding
Polymer({ is: 'my-chooser', properties: { choice: { type: String, notify: true, } }, });
<dom-module id="my-gravatar"> <template> <img src="{{imgsrc}}"> </template> ... </dom-module>
<dom-module id="my-gravatar"> <template> <img id="gravatar"> </template> <script> Polymer({ is: 'my-gravatar', ready: function() { this.$.gravatar.src = '//gravatar.com/avatar/abcdef'; } }); </script> </dom-module>
Local DOM
var toLocal = document.createElement('div'); var beforeNode = Polymer.dom(this.root).childNodes[0]; Polymer.dom(this.root).insertBefore(toLocal, beforeNode);
Light DOM
Polymer.dom(this).appendChild(document.createElement('div')); var allSpans = Polymer.dom(this).querySelectorAll('span');
<dom-module id="my-strongbad"> <template> <strong><content></content></strong> </template> ... </dom-module>
<my-strongbad>Deleted!</my-strongbad>
<dom-module id="my-gravatar"> <template> <input type="text" value={{email::input}}></input> <input type="text" value={{size::input}}></input> <img src="{{imgsrc}}"> </template> ... </dom-module>
<template> <my-gravatar email="[[email]]"></my-gravatar> </template>
<template> <my-chooser choice="{{choice}}"></my-chooser> </template>
Host-To-Child
<template> <my-gravatar email="[[email]]"></my-gravatar> <input type="text" value="{{email::input}}"> </template> <script> Polymer({ is: 'my-element', properties: { email: String, }, }); </script>
Bi-directional between child and host
<template> <my-chooser choice="{{type}}"></my-chooser> </template> <script> Polymer({ is: 'my-element', properties: { type: String, }, }); </script>
Polymer({ is: 'x-custom', listeners: { 'tap': 'regularTap', 'special.tap': 'specialTap' }, regularTap: function(e) { alert("Thank you for tapping"); }, specialTap: function(e) { alert("It was special tapping"); } });
<button on-click="buttonClick">Click Me</button>
<dom-module id="x-custom"> <template> <button on-click="handleClick">Kick Me</button> </template> <script> Polymer({ is: 'x-custom', handleClick: function(e, detail) { this.fire('kick', {kicked: true}); } }); </script> </dom-module>
<template> <style> :host { /* Selector to style the host DOM element */ display: block; } .content-wrapper > ::content .warning { /* Light DOM */ color: red; } </style> <div class="content-wrapper"><content></content></div> </template>
"Theming"
<template> <style> :host { /* Selector to style the host DOM element */ display: block; } .content-wrapper > ::content .warning { /* Light DOM */ color: var(--my-element-warning-color, red); } </style> <div class="content-wrapper"><content></content></div> </template>
<template> <style> :host { /* Selector to style the host DOM element */ display: block; @apply(--my-element-theme); } </style> </template>
<style> :host { --my-element-theme { background-color: green; } } </style>
$ wget https://github.com/PolymerElements/polymer-starter-kit/releases/download/v1.0.3/polymer-starter-kit-1.0.3.zip $ unzip polymer-starter-kit-1.0.3.zip $ cd polymer-starter-kit-1.0.3 $ npm install && bower install $ gulp serve