72
Web Components and Rails Boris Nadion [email protected] @borisnadion

Web Components With Rails

Embed Size (px)

Citation preview

Page 1: Web Components With Rails

Web Components and Rails

Boris Nadion [email protected]

@borisnadion

Page 2: Web Components With Rails

@borisnadion http://astrails.com

Page 3: Web Components With Rails

awesome web and mobile appssince 2005

Page 4: Web Components With Rails

1997

Page 5: Web Components With Rails
Page 6: Web Components With Rails

bank

Page 7: Web Components With Rails

Active DesktopMicrosoft

Page 8: Web Components With Rails

desktop widgets

Page 9: Web Components With Rails

tasks management appvanilla javascript + HTML running in IE4 on a desktop

Page 10: Web Components With Rails

innovation

Page 11: Web Components With Rails

mostly C/C++ until 2005

Page 12: Web Components With Rails

Ruby

Page 13: Web Components With Rails

Rails

Page 14: Web Components With Rails

bin

Page 15: Web Components With Rails

There are only 10 types of people in world: those who understand binary and those who don’t.

Page 16: Web Components With Rails

102types of people in web development

Page 17: Web Components With Rails

backend

Page 18: Web Components With Rails

frontend

Page 19: Web Components With Rails

html

Page 20: Web Components With Rails

html + css

Page 21: Web Components With Rails

html + css + javascript

Page 22: Web Components With Rails

full stack112

Page 23: Web Components With Rails

testwhat engineer are you?

Page 24: Web Components With Rails

2 recent memory leakswhat did they blow?

Page 25: Web Components With Rails

server

2 backend

Page 26: Web Components With Rails

server browser

2 backend

2 frontend

Page 27: Web Components With Rails

server browser

2 backend

2 frontend

1 1 full stack

Page 28: Web Components With Rails

server browser

2 backend

2 frontend

1 1 full stack

designer/PM

Page 29: Web Components With Rails

2005

Page 30: Web Components With Rails

prototype.js + effects.js

Page 31: Web Components With Rails

$(…) with $$(…)$$ lookup is much more expensive than $

Page 32: Web Components With Rails

jQuery2006

Page 33: Web Components With Rails

Angular.js

Page 34: Web Components With Rails

ember.js

Page 35: Web Components With Rails

Backbone/Marionette

Page 36: Web Components With Rails

MVC / MVVM / MV*

Page 37: Web Components With Rails

class  MyApp.SomeController  extends  Marionette.Controller      show:  (id)  -­‐>          App.mainRegion.show  new  MyApp.SomeView(              model:  App.request("somemodel:load",  id)          )  

class  MyApp.SomeView  extends  Marionette.ItemView      template:  "templates/some/show"      templateHelpers:  -­‐>          klass:  =>  if  @model.has_something()  then  "some"  else  "nothing"      initialize:  =>          @listenTo  @model,  "change",  @render  

class  Models.SomeModel  extends  Backbone.Model      url:  -­‐>          Routes.somemodel_path(@get("id"))  

App.reqres.setHandlers      "some  model:load":  (id)  -­‐>          res  =  new  Model.SomeModel(id:  id)          res.fetch()          res

Page 38: Web Components With Rails

document.registerElement

Page 39: Web Components With Rails

XFoo  =  document.registerElement('x-­‐foo')  document.body.appendChild(new  XFoo())

Page 40: Web Components With Rails

proto  =  Object.create(HTMLElement.prototype)  document.registerElement(‘x-­‐foo’,  prototype:  proto)

Page 41: Web Components With Rails

lifecycle callbacks

Page 42: Web Components With Rails

proto  =  Object.create(HTMLElement.prototype)  

proto.createdCallback  =  -­‐>  #...  proto.attachedCallback  =  -­‐>  #...  proto.detachedCallback  =  -­‐>  #...  proto.attributeChangedCallback  =  (name,  from,  to)  -­‐>  #...  

XFoo  =  document.registerElement('x-­‐foo',  prototype:proto)

Page 43: Web Components With Rails

encapsulate functionality

Page 44: Web Components With Rails

proto.createdCallback  =  -­‐>      @addEventListener  'click',  (e)  -­‐>          alert('yeah')

Page 45: Web Components With Rails

photo.createdCallback  =  -­‐>      @innerHTML  =  "<b>I'm  bold  and  inside  the  x-­‐foo</b>"

Page 46: Web Components With Rails

Shadow Root

Page 47: Web Components With Rails

proto.createdCallback  =  -­‐>      shadow  =  @createShadowRoot()      shadow.innerHTML  =  "<b>I'm  bold  and  inside  the  shadow  root</b>"

Page 48: Web Components With Rails

Templates

Page 49: Web Components With Rails

proto.createdCallback  =  -­‐>      template  =  document.querySelector('#mytemplate')      clone  =  document.importNode(template.content,  true)      @createShadowRoot().appendChild(clone)  

<template  id="mytemplate">      <style>          p  {  color:  yellow;  }      </style>      <p>I'm  yellow  in  a  shadow  root</p>  </template>

Page 50: Web Components With Rails

Polymerhttps://www.polymer-project.org/

Page 51: Web Components With Rails

<!-­‐-­‐  Define  element  -­‐-­‐>  <polymer-­‐element  name="my-­‐counter"  attributes="counter">      <template>          <style>  /*...*/  </style>          <div  id="label"><content></content></div>          Value:  <span  id="counterVal">{{counter}}</span><br>          <button  on-­‐tap="{{increment}}">Increment</button>      </template>      <script>          Polymer({              counter:  0,  //  Default  value              counterChanged:  function()  {                  this.$.counterVal.classList.add('highlight');              },              increment:  function()  {                  this.counter++;              }          });      </script>  </polymer-­‐element>  

<!-­‐-­‐  Use  element  -­‐-­‐>  <my-­‐counter  counter="10">Points</my-­‐counter>

Page 52: Web Components With Rails

X-Tagshttp://x-tags.org/

Page 53: Web Components With Rails

xtag.register  "x-­‐accordion",      extends:  "div"      lifecycle:          created:  -­‐>          inserted:  -­‐>          removed:  -­‐>          attributeChanged:  -­‐>      events:          "click:delegate(x-­‐toggler)":  -­‐>      accessors:          togglers:              get:  -­‐>              set:  (value)  -­‐>      methods:          nextToggler:  -­‐>          previousToggler:  -­‐>

Page 54: Web Components With Rails

Bosonichttp://bosonic.github.io/

Page 55: Web Components With Rails

<element  name="b-­‐hello-­‐world">          <style>                  </style>          <template>                  <h3>Hello,  world!</h3>                  <p>Lorem  ipsum</p>          </template>          <script>                  ({                          createdCallback:  function()  {                                  var  root  =  this.createShadowRoot();                                root.appendChild(this.template.content.cloneNode(true));                          }                  });          </script>  </element>

Page 56: Web Components With Rails

Brickhttp://mozbrick.github.io/

Page 57: Web Components With Rails

Polymerhttps://www.polymer-project.org/

Page 58: Web Components With Rails
Page 59: Web Components With Rails

polyfillsupport old browsers

Page 60: Web Components With Rails

<!-­‐-­‐  Polyfill  Web  Components  support  for  older  browsers  -­‐-­‐>  <script  src="components/platform/platform.js"></script>  

<!-­‐-­‐  Import  element  -­‐-­‐>  <link  rel="import"  href="google-­‐map.html">  

<!-­‐-­‐  Use  element  -­‐-­‐>  <google-­‐map  lat="37.790"  long="-­‐122.390"></google-­‐map>

Page 61: Web Components With Rails

lightweight CEShttps://github.com/WebReflection/document-register-element

Page 62: Web Components With Rails

• custom elements • html imports • templates • shadow root

Page 63: Web Components With Rails

Polymer Railshttps://github.com/alchapone/polymer-rails

Page 64: Web Components With Rails
Page 65: Web Components With Rails

//  application.html  //=  require  polymer/polymer  //=  require  my-­‐navigation/my-­‐navigation  

//  my-­‐navigation.html.erb  <polymer-­‐element  name="my-­‐navigation">      <template>          <%=  stylesheet_link_tag  'my-­‐navigation/style'%>          <ul  class="navigation">              <li><a  href="/">Home</a></li>              <li><a  href="/">Contacts</a></li>              <li><a  href="/">Blog</a></li>              <li><a  href="/">Twitter</a></li>              <li><a  href="/">Facebook</a></li>          </ul>      </template>  

<%=  javascript_include_tag  "my-­‐navigation/my-­‐navigation"%>  </polymer-­‐element>  

//  my-­‐navigation.coffee  Polymer  'my-­‐navigation'  

//  style.sass  \:host      display:  block  

.navigation      list-­‐style:  none      margin:  0      background:  #fff  #  ...

Page 66: Web Components With Rails

Should I use Polymer for my next project?https://news.ycombinator.com/item?id=7970781

Page 67: Web Components With Rails

encapsulation

Page 68: Web Components With Rails

clean up HTML

Page 69: Web Components With Rails

hide everythingshadow DOM

Page 70: Web Components With Rails

HTML vs. MV*

Page 71: Web Components With Rails

frontend is cool again

Page 72: Web Components With Rails

thanks!

Boris Nadion astrails.com/blog