web components + backbone: a game-changing combination

67
Web Components + Backbone A Game-Changing Combination

Upload: andrew-rota

Post on 12-Jul-2015

21.321 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Web Components + Backbone: a Game-Changing Combination

Web Components + BackboneA Game-Changing Combination

Page 2: Web Components + Backbone: a Game-Changing Combination

Who Am I?

Andrew RotaJavaScript Engineer,

Page 3: Web Components + Backbone: a Game-Changing Combination

JavaScript Modularity

By using small libraries –components with a dedicatedpurpose and a small surfacearea – it becomes possible topick and mix, to swap parts ofour front end stack... - Jimmy Breck-McKye, "The State of JavaScript in2015"

Page 4: Web Components + Backbone: a Game-Changing Combination

Modularity in HTML == DOM Elements

Page 5: Web Components + Backbone: a Game-Changing Combination

<ul>  <li>First Item</li>  <li>Second Item</li></ul>

Page 6: Web Components + Backbone: a Game-Changing Combination

<ol>  <li>First Item</li>  <li>Second Item</li></ol>

Page 7: Web Components + Backbone: a Game-Changing Combination

<div class="header"></div>

Page 8: Web Components + Backbone: a Game-Changing Combination

<header></header>

Page 9: Web Components + Backbone: a Game-Changing Combination

<div id="nav"></div>

Page 10: Web Components + Backbone: a Game-Changing Combination

<nav></nav>

Page 11: Web Components + Backbone: a Game-Changing Combination

Libraries > frameworks?- Jimmy Breck-McKye, "The State of JavaScript in2015"

Page 12: Web Components + Backbone: a Game-Changing Combination

Libraries > frameworks?- Jimmy Breck-McKye, "The State of JavaScript in2015"

Native functionality > libraries> frameworks.- Me

Page 13: Web Components + Backbone: a Game-Changing Combination

But creating your own elementsisn't possible...

Page 14: Web Components + Backbone: a Game-Changing Combination

... until now.

Page 15: Web Components + Backbone: a Game-Changing Combination

Web Componentsusher in a new era ofweb developmentbased onencapsulated andinteroperable customelements that extendHTML itself. - Polymer Project

Web Components

Page 16: Web Components + Backbone: a Game-Changing Combination

Web Component Technologies

Custom Elements

HTML Templates

HTML Imports

Shadow DOM

Page 17: Web Components + Backbone: a Game-Changing Combination

Web Component Technologies

Custom Elements

HTML Templates

HTML Imports

Shadow DOM

Page 18: Web Components + Backbone: a Game-Changing Combination

Custom Elements

<my‐element>Hello World.</my‐element>            

var MyElement = document.registerElement('my‐element', {  prototype: Object.create(HTMLElement.prototype)});            

Page 19: Web Components + Backbone: a Game-Changing Combination

Web Component Technologies

Custom Elements

HTML Templates

HTML Imports

Shadow DOM

Page 20: Web Components + Backbone: a Game-Changing Combination

HTML Templates<template id="my‐template">    <p>Hello World.</p>    <!‐‐ This image won't be downloaded on page load ‐‐>    <img src="example.jpg" alt="Example"></template>

document.importNode(    document.getElementById('my‐template').content,    true);

Page 21: Web Components + Backbone: a Game-Changing Combination

Web Component Technologies

Custom Elements

HTML Templates

HTML Imports

Shadow DOM

Page 22: Web Components + Backbone: a Game-Changing Combination

HTML Imports<link rel="import" href="/imports/my‐component.html">

Page 23: Web Components + Backbone: a Game-Changing Combination

Web Component Technologies

Custom Elements

HTML Templates

HTML Imports

Shadow DOM

Page 24: Web Components + Backbone: a Game-Changing Combination

Browser Shadow DOM

Page 25: Web Components + Backbone: a Game-Changing Combination

Shadow DOM<div id="my‐element"></div><p>Light DOM.</p>

// Create Shadow Rootvar s = document.getElementById('my‐element').createShadowRoot();// Add Styles and Texts.innerHTML += '<style>p { color: crimson; }</style>';s.innerHTML += '<p>Shadow DOM.</p>';

Shadow DOM.

Light DOM.

Page 26: Web Components + Backbone: a Game-Changing Combination

<content><div id="my‐element"><p>Hello!</p></div>

var s = document.getElementById('my‐element').createShadowRoot();s.innerHTML += '<p>Shadow DOM Start.</p>';s.innerHTML += '<style>p { color: crimson; }</style>';s.innerHTML += '<content></content>';s.innerHTML += '<p>Shadow DOM End.</p>';

Shadow DOM Start.Hello!

Shadow DOM End.

Page 27: Web Components + Backbone: a Game-Changing Combination

Web Component Technologies

Custom Elements

HTML Templates

HTML Imports

Shadow DOM

Page 28: Web Components + Backbone: a Game-Changing Combination

What Web Components Lack...

Application Structure

Server Interface

URL Router

Models/Collections + Events

Page 29: Web Components + Backbone: a Game-Changing Combination

...We Gain with Backbone

Application Structure

Server Interface

URL Router

Models/Collections + Events

Page 30: Web Components + Backbone: a Game-Changing Combination

Using WebComponent

Technologies+

Backbone

Page 31: Web Components + Backbone: a Game-Changing Combination

Backbone + Custom Elementsdocument.registerElement('my‐custom‐element', {  prototype: Object.create(HTMLElement.prototype)});

Backbone.View.extend({  tagName: 'my‐custom‐element'});

Page 32: Web Components + Backbone: a Game-Changing Combination

Backbone + HTML TemplatesBackbone.View.extend({  template: document.importNode(    document.getElementById('my‐template').content,    true  ),  render: function() {    this.el.innerHTML = this.template;  }});

Page 33: Web Components + Backbone: a Game-Changing Combination

Backbone + HTML Imports<link rel="import" href="my‐custom‐component.html">

Page 34: Web Components + Backbone: a Game-Changing Combination

Backbone + Shadow DOMBackbone.View.extend({  initialize: function() {    this.el.createShadowRoot();  }});

Page 35: Web Components + Backbone: a Game-Changing Combination

Using WebComponent

Technologies+

Backbone

Page 36: Web Components + Backbone: a Game-Changing Combination

Using WebComponents

+Backbone

Page 37: Web Components + Backbone: a Game-Changing Combination

polymer-project.org/docs/elements

Page 38: Web Components + Backbone: a Game-Changing Combination

x-tags.org

Page 39: Web Components + Backbone: a Game-Changing Combination

component.kitchen

Page 40: Web Components + Backbone: a Game-Changing Combination

customelements.io

Page 41: Web Components + Backbone: a Game-Changing Combination

Backbone View+

Web Component

Page 42: Web Components + Backbone: a Game-Changing Combination

<paper‐toast>

Page 43: Web Components + Backbone: a Game-Changing Combination

<paper‐toast> API

<paper‐toast  text="Your toast is ready!"  duration="5000"  autoCloseDisabled  opened></paper‐toast>

Element.show();Element.dismiss();Element.toggle();

Element.addEventListener('core‐overlay‐open‐completed', doSomething

Page 44: Web Components + Backbone: a Game-Changing Combination

Backbone.View.extend({  tagName: 'paper‐toast',  attributes: {    text: 'Your toast is ready!',    autoCloseDisabled: true,    duration: '5000',    opened: true  },  events: {    'core‐overlay‐open‐completed': 'doSomething'  },  toggle: function() {    this.el.toggle();  }});

Page 45: Web Components + Backbone: a Game-Changing Combination

<google‐map>

Page 46: Web Components + Backbone: a Game-Changing Combination

<google‐map> API

<google‐map  zoom="10"  latitude="42.3581"  longitude="‐71.0636"></google‐map>

Element.resize();Element.clear();

Element.addEventListener('google‐map‐ready', doSomething);

Page 47: Web Components + Backbone: a Game-Changing Combination

Backbone.View.extend({  tagName: 'google‐map',  attributes: {    latitude: '42.3581',    longitude: '‐71.0636',    zoom: '10'  },  events: {    'google‐map‐ready': 'doSomething'  },  resize: function() {    this.el.resize();  }});

Page 48: Web Components + Backbone: a Game-Changing Combination

Backbone.View.extend({  initialize: function() {    this.listenTo(this.model, 'change', this.moveMap );  },  tagName: 'google‐map',  moveMap: function(model) {    this.el.setAttribute('latitude', this.model.get('lat'));    this.el.setAttribute('longitude', this.model.get('long'));    this.el.setAttribute('zoom', this.model.get('zoom'));  }});

Page 49: Web Components + Backbone: a Game-Changing Combination
Page 50: Web Components + Backbone: a Game-Changing Combination

Building Web Componentsfor Backbone (or anything else)

Page 51: Web Components + Backbone: a Game-Changing Combination

├── hello‐world├──── hello‐world.html├──── hello‐world.js└──── bower.json            

Page 52: Web Components + Backbone: a Game-Changing Combination

<template id="my‐template">  Hello, <content></content</template><script src="hello‐world.js

Page 53: Web Components + Backbone: a Game-Changing Combination

var element = Object.create(HTMLElement.prototype);

element.createdCallback = function() {};

element.attachedCallback = function() {};

element.attributeChangedCallback = function(attr, oldVal, newVal) {}

element.detachedCallback = function() {};

document.registerElement('hello‐world', {  prototype: element});

Page 54: Web Components + Backbone: a Game-Changing Combination

<link  rel="import"  href="components/hello‐world/hello‐world.html>

<!‐‐ [...] ‐‐>

<hello‐world>I'm a web component</

Page 55: Web Components + Backbone: a Game-Changing Combination

How It All Fits Together

Page 56: Web Components + Backbone: a Game-Changing Combination

Application + Components

Application

Component Component Component Component Component

Component Component Component Component Component

Component Component Component Component Component

Page 57: Web Components + Backbone: a Game-Changing Combination

Application + Components

Page 58: Web Components + Backbone: a Game-Changing Combination

Application + Components

Page 59: Web Components + Backbone: a Game-Changing Combination

Application + Components

< X >

< X >

Page 60: Web Components + Backbone: a Game-Changing Combination

Web Component All the Things??<backboneconf‐app>    <backboneconf‐menu></backboneconf‐menu>    <backboneconf‐content></backboneconf‐content>    <backboneconf‐footer></backboneconf‐footer></backboneconf‐app>

Page 61: Web Components + Backbone: a Game-Changing Combination

Probably Not (and that's OK)

I don't ever see us going all inon Custom Elements for everypossible thing ... Use nativeelements and controls whenpossible and supplement withcustom elements. - Joshua Peek, Github Programmer

Page 62: Web Components + Backbone: a Game-Changing Combination

Should I Componentize?Does it encapsulate component-level logic?

Does it take the place of a native element?

Should it be portable?

Is it context independent?

Can the API be represented as attributes, methods, and events?

Page 63: Web Components + Backbone: a Game-Changing Combination

Small

Open for Extension

Documented

Unit Tested

Accessible

Idempotent

Best Practices

Page 64: Web Components + Backbone: a Game-Changing Combination

Can I Use???Custom

ElementsHTML

TemplatesHTML

ImportsShadow

DOM

✓ ✓ ✓ ✓

✓ ✓ ✓ ✓

Flag ✓ Flag Flag

X ✓ X X

X X X X

Page 65: Web Components + Backbone: a Game-Changing Combination

Can I Use???Custom

ElementsHTML

TemplatesHTML

ImportsShadow

DOM

✓ ✓ ✓ ✓

✓ ✓ ✓ ✓

✓ ✓ ✓ ✓

✓ ✓ ✓ ✓

✓ ✓ ✓ ✓

webcomponents.js

Page 66: Web Components + Backbone: a Game-Changing Combination

Towards a Component Driven Web

Page 67: Web Components + Backbone: a Game-Changing Combination

Thanks!

Resources- WebComponents.org- Web Components: A Tectonic Shift for Web Development by Eric Bidelman- Web Components by Jarrod Overson and Jason Strimpel- Ten Principles for Great General Purpose Web Components

Colophon

This presentation was built with Backbone.js, Shadow DOM, HTMLTemplates, HTML Imports, and the Custom Element <slide‐content>using Web Component Slides.