PolymerJS 開發實戰

32
你不能不知道的 PolymerJS 開發實戰 2014-08-30 Marty
  • Upload

    -
  • Category

    Software

  • view

    370
  • download

    6

description

Web 開發元件化有其優勢與應用場景。PolymerJS 是 Google 打造支援 Web Compoent 標準的實作框架,雖然 AngularJS 有 directive 來提供元件開發與應用,但這點如果和 PolymerJS 比起來,PolymerJS 其實更為全面且出色。 今年 Google 2014 開發者大會推出 Material Design 後,PolymerJS 率先支援了 Material Design 元件組,筆者小小玩一下感覺很 high 啊

Transcript of PolymerJS 開發實戰

Page 1: PolymerJS 開發實戰

你不能不知道的 PolymerJS 開發實戰

2014-08-30 Marty

Page 2: PolymerJS 開發實戰

分享項目

● 建置本機 Polymer 環境● 打造自己的HTML標籤● 樣板+雙向繫結● 武器庫● 資源

Page 3: PolymerJS 開發實戰

WebCompoents

這是瀏覽器都要支持的約定...

Page 4: PolymerJS 開發實戰

WebCompoents

但是現況是...

Page 5: PolymerJS 開發實戰

IE反應是慢了一些

Page 6: PolymerJS 開發實戰

WebComponents 支援程度

Page 7: PolymerJS 開發實戰

IE反應是慢了一些

Page 8: PolymerJS 開發實戰

WebComponents 支援程度 (platform.js+)

Page 9: PolymerJS 開發實戰

很明顯,Polymer 幹的是苦差事 (看很遠)

"X-Tag and Polymer are both high-level sugar libraries that build upon the W3 Web Components specs - each introduces a different approach to making development of web components an even more amazing experience. To help make this more relatable, consider the following: jQuery : DOMX-Tag/Polymer : WebComponents."

Page 10: PolymerJS 開發實戰

本機環境建置 1/3 安裝 NodeJS、Bower

1. 裝 nodejs2. npm install -g bower3. 裝本機Server npm install -g serve

(或python -m SimpleHTTPServer)

https://www.youtube.com/watch?v=1rz334A8U7o

Page 11: PolymerJS 開發實戰

本機環境建置 2/3 安裝 Polymer

//先建個目錄

mkdir polymer

//進去polymer目錄,進行bower初始化,也就是產生bower.json

bower init

//裝 paper元件(Material Design)就好了bower install --save Polymer/paper-elements

//裝 Google 元件 <google-map> (http://googlewebcomponents.github.io)bower install --save GoogleWebComponents/google-map

Page 12: PolymerJS 開發實戰

polymer 框架和元件們

Page 13: PolymerJS 開發實戰

本機環境建置 3/3 (確認)

<!DOCTYPE html>

<html>

<head>

<script src="bower_components/platform/platform.js"></script>

</head>

<body>

<link rel="import"href="bower_components/google-map/google-map.html">

<style>

google-map {

display: block;

height: 600px;

}

</style>

<google-map latitude="22.605153" longitude="120.301125"

disableDefaultUI showCenterMarker zoom="18"></google-map>

</body>

</html>

Page 15: PolymerJS 開發實戰

先嘗鮮 <template> bind

Page 16: PolymerJS 開發實戰
Page 18: PolymerJS 開發實戰

打造自己的 Element <post-card>

<script src="bower_components/platform/platform.js"></script><link rel="import" href="bower_components/core-icons/core-icons.html"><link rel="import" href="bower_components/paper-tabs/paper-tabs.html"><link rel="import" href="bower_components/paper-tabs/paper-tab.html">

Page 19: PolymerJS 開發實戰

打造自己的 Element

Any <h2> children match the second <content> tag and are inserted here.

<post-card>...</post-card>

insert pointer

Page 21: PolymerJS 開發實戰

實戰#2 - template + data-binding

Page 22: PolymerJS 開發實戰

● HTML5 new Tag - <template>● Object.observe() - Polymer data-binding

實戰#2 - template + data-binding

Page 24: PolymerJS 開發實戰

IE反應是慢了一些...

Page 25: PolymerJS 開發實戰

template 用法 (搭配Polymer data-binding)

<paper-tabs selected="0" role="tablist" id="tab"> <template id="tabItem" repeat="{{tabs}}"> <paper-tab role="tab">{{name}}</paper-tab> </template></paper-tabs>

var models = { tabs: [ {name: 't1' }, { name: 't2'}, {name: 't3' }]};

$(document).on('polymer-ready', function () { $('#tabItem')[0].model = models;

$('#add').on('click', function () { models.tabs.push({ name: 'NewTab' }); });

$('#del').on('click', function () { models.tabs.pop(); });});

對model操作自動綁定UI

Page 26: PolymerJS 開發實戰

感受一下 paper 元件和雙向綁定

Link

Note: The <template> element is a new element in the HTML standard. For information on using templates outside of Polymer, see HTML’s New Template Tag on HTML5Rocks.

Link

Dynamic,

two-way data binding

Page 28: PolymerJS 開發實戰

人臉追蹤、聲音播放/辨識

trackingjs.js

voices Web Component wrapper to the Web

Speech API, that allows you to do voice

recognition and speech synthesis using

Polymer

Page 30: PolymerJS 開發實戰

Step1 改成 bind model + draggable='true'

Page 31: PolymerJS 開發實戰

Step2 fix google-map updatePosition(...)