Weitere ähnliche Inhalte Ähnlich wie The Structure of Web Code: A Case For Polymer, November 1, 2014 (20) Kürzlich hochgeladen (20) The Structure of Web Code: A Case For Polymer, November 1, 20149. @polymer #itshackademic
Contents of section .text:
401c40: mov $0x404ae0,%esi
401c45: callq 401180 <dcgettext@plt>
401c4a: mov %rbp,%rdx
401c4d: mov %rax,%rsi
401c50: mov $0x1,%edi
401c55: xor %eax,%eax
401c57: callq 401330 <__printf_chk@plt>
Contents of section .rodata:
404ae0: ascii "For complete documentation, run: "
" info coreutils '%s invocation'n"
404b21: ascii "echo"
404b26: ascii "Report %s …
13. I don't always
structure my frontend code,
but when I do,
I use web components.
@polymer #itshackademic
https://imgflip.com/memegenerator/
The-Most-Interesting-Man-In-The-World
16. <paper-tabs selected=“1”>
<paper-tab>Tab 1</paper-tab>
<paper-tab>Tab 2</paper-tab>
<paper-tab>Tab 3</paper-tab>
</paper-tabs>
@polymer #itshackademic
Custom Elements
define new HTML
declarative, readable
meaningful HTML
extensible → reusable
17. @polymer #itshackademic
Custom Elements
define new HTML
declarative, readable
meaningful HTML
extensible → reusable
var tabs = document.querySelector('paper-tabs');
tabs.addEventListener('core-activate', function() {
console.log(this.selected);
});
19. @polymer #itshackademic
<template>
<div class=“comment”>
<img src=“image.png”>
</div>
<script>…</script>
</template>
HTML Templates
native client-side templates
use DOM to scaffold DOM → no XSS
parsed, not rendered
content is inert until cloned/used
doc fragment → not part of the page
24. <template>
<img src=“{{src}}”>
</template>
<script>
… this.src = 'lolcat.jpg'; …
</script>
@polymer #itshackademic
Mutation Observers
declarative programming
data-binding minimal DOM manipulation
safe behind-the-scenes escaping
two-way img.src = 'otherlolcat.jpg';
integrated use in document and code
26. @polymer #itshackademic
<link rel="stylesheet" href="bootstrap.css">
<link rel="stylesheet" href="fonts.css">
<script src="jquery.js"></script>
<script src="bootstrap.js"></script>
<script src="bootstrap-tooltip.js"></script>
<script src="bootstrap-dropdown.js"></script>
28. Web Components
● Custom Elements
● Templates
● Shadow DOM
● Mutation/Object Observers
● HTML Imports
29. Good Design
requires
good implementation
http://www.reddit.com/r/CrappyDesign/
comments/2k1ima
/would_it_make_it_weird_if_i_closed_the_stall_door/
37. <link rel="import" href="../polymer/polymer.html">
<polymer-element name="traffic-light" attributes="color">
<template>
@polymer #itshackademic
<style>
div {
border-radius: 100px;
height: 200px;
width: 200px;
}
</style>
<div style="background-color: {{ color }}"></div>
</template>
<script>
Polymer({
color: 'blue'
});
</script>
</polymer-element>
39. <link rel="import" href="../traffic-light/traffic-light.html">
<polymer-element name="traffic-app" attributes="activeColor">
<template>
<link rel="stylesheet" href="traffic-app.css">
<traffic-light color="red"></traffic-light>
<traffic-light color="yellow"></traffic-light>
<traffic-light color="green"></traffic-light>
</template>
<script name="traffic-app.js"></script>
</polymer-element>
@polymer #itshackademic
40. <link rel="import" href="../traffic-light/traffic-light.html">
<polymer-element name="traffic-app" attributes="activeColor">
<template>
<link rel="stylesheet" href="traffic-app.css">
<template repeat="{{ color in lights }}">
<traffic-light color="{{ color }}"></traffic-light>
</template>
</template>
@polymer #itshackademic
<script>
Polymer({
activeColor: 'red',
lights: [ 'red', 'yellow', 'green' ]
});
</script>
</polymer-element>
45. @polymer #itshackademic
}
</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!
60. ○ https://www.polymer-project.org/
○ https://www.polymer-project.org/docs/start/creatingelements.html
○ https://www.polymer-project.org/tools/designer/
○ https://github.com/Polymer/polymer-expressions
○ http://itshackademic.com/codelabs
○ http://www.w3.org/TR/custom-elements/
○ http://www.w3.org/TR/shadow-dom/
○ http://www.w3.org/TR/html5/scripting-1.html#the-template-element
○ https://dom.spec.whatwg.org/#mutation-observers
○ http://wiki.ecmascript.org/doku.php?id=harmony:observe
○ www.w3.org/TR/html-imports/
○ http://blog.stevensanderson.com/2014/08/18/knockout-3-2-0-released/
○ http://www.x-tags.org/