Upgrade to Pro — share decks privately, control downloads, hide ads and more …

DevFest2014-Zhuhai-Polymer

Zoom.Quiet
November 17, 2014

 DevFest2014-Zhuhai-Polymer

官方幻灯转载:
配合中文讲解录像...

Zoom.Quiet

November 17, 2014
Tweet

More Decks by Zoom.Quiet

Other Decks in Technology

Transcript

  1. <paper-tabs selected=“1”> <paper-tab>Tab 1</paper-tab> <paper-tab>Tab 2</paper-tab> <paper-tab>Tab 3</paper-tab> </paper-tabs> declarative,

    readable meaningful HTML common way to extend → reusable Custom Elements define new HTML @polymer #itshackademic
  2. declarative, readable meaningful HTML common way to extend → reusable

    Custom Elements define new HTML var tabs = document.querySelector('paper-tabs'); tabs.addEventListener('core-activate', function() { console.log(this.selected); }); @polymer #itshackademic
  3. <template> <div class=“comment”> <img src=“image.png”> </div> <script>...</script> </template> use DOM

    to scaffold DOM → no XSS content is inert until cloned/used doc fragment → not part of the page HTML Templates native client-side templates parsed, not rendered @polymer #itshackademic
  4. Custom Elements Create new HTML elements and extend existing ones

    Templates Native templating in the browser Shadow DOM Scoped CSS!!! + encapsulated markup HTML Imports Load custom element definitions and resources
  5. vanilla polymer <polymer-element name=“paper-tabs”> … </polymer-element> usage <paper-tabs>…</paper-tabs> // document.createElement(‘paper-tabs’);

    document.registerElement(‘paper-tabs’, { prototype: Object.create(HTMLElement.prototype) }); @polymer #itshackademic
  6. document.registerElement(‘paper-tabs’, { prototype: Object.create(HTMLElement.prototype) }); vanilla polymer <polymer-element name=“paper-tabs”> …

    </polymer-element> usage <paper-tabs>…</paper-tabs> // document.createElement(‘paper-tabs’); @polymer #itshackademic
  7. vanilla polymer <polymer-element name=“paper-tabs”> … </polymer-element> usage <paper-tabs>…</paper-tabs> // document.createElement(‘paper-tabs’);

    document.registerElement(‘paper-tabs’, { prototype: Object.create(HTMLElement.prototype) }); @polymer #itshackademic
  8. vanilla polymer <polymer-element name=“paper-tabs”> … </polymer-element> usage <paper-tabs>…</paper-tabs> // document.createElement(‘paper-tabs’);

    document.registerElement(‘paper-tabs’, { prototype: Object.create(HTMLElement.prototype) }); @polymer #itshackademic
  9. vanilla polymer <polymer-element name=“super-button” extends=“button”> … </polymer-element> usage <button is=“super-button”>…</button>

    // document.createElement(‘button’, ‘super-button’); document.registerElement(‘super-button’, { prototype: Object.create(HTMLButtonElement.prototype), extends: ‘button’ }); @polymer #itshackademic
  10. vanilla polymer <polymer-element name=“super-button” extends=“button”> … </polymer-element> usage <button is=“super-button”>…</button>

    // document.createElement(‘button’, ‘super-button’); document.registerElement(‘super-button’, { prototype: Object.create(HTMLButtonElement.prototype), extends: ‘button’ }); @polymer #itshackademic
  11. vanilla polymer <polymer-element name=“super-button” extends=“button”> … </polymer-element> usage <button is=“super-button”>…</button>

    // document.createElement(‘button’, ‘super-button’); document.registerElement(‘super-button’, { prototype: Object.create(HTMLButtonElement.prototype), extends: ‘button’ }); @polymer #itshackademic
  12. vanilla polymer <polymer-element name=“super-button” extends=“button”> … </polymer-element> usage <button is=“super-button”>…</button>

    // document.createElement(‘button’, ‘super-button’); document.registerElement(‘super-button’, { prototype: Object.create(HTMLButtonElement.prototype), extends: ‘button’ }); @polymer #itshackademic
  13. vanilla polymer <polymer-element name=“user-list” noscript> <template> <ul> <template repeat=“{{user, i

    in users}}”> <li>{{user.name}}</li> </template> </ul> </template> </polymer-element> <template> … </template> @polymer #itshackademic
  14. vanilla polymer <polymer-element name=“user-list” noscript> <template> <ul> <template repeat=“{{user, i

    in users}}”> <li>{{user.name}}</li> </template> </ul> </template> </polymer-element> <template> … </template> @polymer #itshackademic
  15. vanilla polymer <polymer-element name=“user-list” noscript> <template> <ul> <template repeat=“{{user, i

    in users}}”> <li>{{user.name}}</li> </template> </ul> </template> </polymer-element> <template> … </template> @polymer #itshackademic
  16. var shadow = el.createShadowRoot(); shadow.innerHTML = “<style>h2 { color: red;

    }</style>” + “<h2>I’m a profile-card</h2>”; vanilla <polymer-element name=“profile-card” noscript> <template> <link rel=“stylesheet” href=“styles.css”> <h2>I’m a profile-card</h2> </template> </polymer-element> polymer @polymer #itshackademic
  17. var shadow = el.createShadowRoot(); shadow.innerHTML = “<style>h2 { color: red;

    }</style>” + “<h2>I’m a profile-card</h2>”; vanilla <polymer-element name=“profile-card” noscript> <template> <link rel=“stylesheet” href=“styles.css”> <h2>I’m a profile-card</h2> </template> </polymer-element> polymer @polymer #itshackademic
  18. var shadow = el.createShadowRoot(); shadow.innerHTML = “<style>h2 { color: red;

    }</style>” + “<h2>I’m a profile-card</h2>”; vanilla <polymer-element name=“profile-card” noscript> <template> <link rel=“stylesheet” href=“styles.css”> <h2>I’m a profile-card</h2> </template> </polymer-element> polymer @polymer #itshackademic
  19. A simple container with a header section and a content

    section <core-header-panel> MY APP <core-header-panel flex> <core-toolbar> <core-icon-button icon=“menu"> </core-icon-button> <div>MY APP</div> </core-toolbar> <div class=“content”>…</div> </core-header-panel>
  20. A simple container with a header section and a content

    section <core-header-panel> MY APP <core-header-panel flex> <core-toolbar> <core-icon-button icon=“menu"> </core-icon-button> <div>MY APP</div> </core-toolbar> <div class=“content”>…</div> </core-header-panel>
  21. A simple container with a header section and a content

    section <core-header-panel> MY APP <core-header-panel flex> <core-toolbar> <core-icon-button icon=“menu"> </core-icon-button> <div>MY APP</div> </core-toolbar> <div class=“content”>…</div> </core-header-panel>
  22. A simple container with a header section and a content

    section <core-header-panel> <core-header-panel flex> <core-toolbar> <core-icon-button icon=“menu"> </core-icon-button> <div>MY APP</div> </core-toolbar> <div class=“content”>…</div> </core-header-panel> MY APP
  23. <core-header-panel mode=“scroll" flex> <core-toolbar> <core-icon-button icon=“menu"> </core-icon-button> <div>MY APP</div> </core-toolbar>

    <div class=“content”>…</div> </core-header-panel> <core-header-panel> Toolbar will scroll with the page
  24. A responsive container that combines a left- or right-side drawer

    panel and a main content area. <core-drawer-panel> <core-drawer-panel> <div drawer> Drawer panel... </div> <div main> Main panel... </div> </core-drawer-panel>
  25. <core-drawer-panel> <div drawer> Drawer panel... </div> <div main> Main panel...

    </div> </core-drawer-panel> A responsive container that combines a left- or right-side drawer panel and a main content area. <core-drawer-panel>
  26. <core-drawer-panel> <div drawer> Drawer panel... </div> <div main> Main panel...

    </div> </core-drawer-panel> A responsive container that combines a left- or right-side drawer panel and a main content area. <core-drawer-panel>
  27. <paper-shadow z=“5” animated> <div class=“card”>...</div> </paper-shadow> A dynamic shadow for

    conveying z-depth and spatial relationships <paper-shadow> @polymer #itshackademic
  28. <paper-slider min=“0” max=“100”> </paper-slider> allows you to style nodes internal

    to an element’s shadow dom ::shadow @polymer #itshackademic
  29. allows you to style nodes internal to an element’s shadow

    dom ::shadow paper-slider::shadow #sliderKnobInner { background-color: #f4b400; } <paper-slider min=“0” max=“100”> </paper-slider> @polymer #itshackademic
  30. html /deep/ paper-ripple { background-color: #E91E63; } styles will pierce

    all shadow boundaries /deep/ @polymer #itshackademic
  31. <app-router> github.com/erikringsmuth/app-router my-site.com/order/:id <app-router> <!-- matches an exact path -->

    <app-route path="/home" import="/pages/home-page.html"></app-route> <!-- matches using a path variable --> <app-route path="/order/:id" import=“/pages/order-page.html"></app-route> </app-router>
  32. <page-er> github.com/addyosmani/page-er <page-er perpage="5" previous=“<< Previous" next=“Next >>"></page-er> var pager

    = document.querySelector("page-er"); document.addEventListener("polymer-ready", function() { pager.data = model.items; });
  33. <ajax-form> github.com/garstasio/ajax-form Full Name Country City Join newsletter <form is="ajax-form"

    action="my/form/handler"> <label>Full Name <input type="text" name=“full_name"> </label> … </form> √
  34. I want to add a marker to a Google map.

    @polymer #itshackademic
  35. } </style> <div id="map"></div> <script src=“http://maps.googleapis.com/maps/api/js?callback=mapReady"> </script> <script> var marker

    = null; function getCurrentLocation(callback) { navigator.geolocation.watchPosition(callback); } function addMarker(opts, info) { var marker = new google.maps.Marker(opts); var infoWindow = new google.maps.InfoWindow({content: info}); google.maps.event.addListener(marker, 'click', function() { infoWindow.open(opts.map, marker); }); return marker; } function mapReady() { var container = document.querySelector('#map'); var map = new google.maps.Map(container, { zoom: 14, disableDefaultUI: true }); getCurrentLocation(function(pos) { var current = new google.maps.LatLng(pos.coords.latitude, pos.coords.longitude); map.setCenter(current); // Re-position marker or create new one. if (marker) { marker.setPosition(map.getCenter()); } else { marker = addMarker({ position: current, map: map, title: 'Your location' }, '<b>Your location</b>'); } So much code for one map marker! @polymer #itshackademic
  36. { "name": "my-element", "version": "0.0.0", "description": "My awesome Custom Element",

    "license": "MIT", "keywords": [ "web-components" ], "ignore": [ "**/.*", "node_modules", "bower_components" ] bower.json @polymer #itshackademic