polymer web componets 大前端
大前端
東南水鄉(xiāng) 一葉小舟拂過水面 船上兩位大俠把酒言歡 一位是玉真人 另一位是張真人 兩人喝到開心處 變作對聯(lián)起來 上聯(lián) 前端研究,研究個屁~ 下聯(lián) 前端設(shè)計,設(shè)計個屁~ 橫批 前端sb
特色
polymer 提供創(chuàng)建自定義和標準dom元素類似的自定義元素功能
可以使用constructor或者document.createElement創(chuàng)建元素
可以配置元素attributes或properties
可以在標簽內(nèi)部定義一些dom
可以對屬性和屬性的變化處理
可以有一些默認的樣式,在外部修內(nèi)部樣式
可以提供方法允許你來操縱它的內(nèi)部狀態(tài)
一個基本的polymer元素定義如下:
<dom-module id="element-name"><style>/* CSS rules for your element */</style><template><!-- local DOM for your element --><div>{{greeting}}</div> <!-- data bindings in local DOM --></template> </dom-module><script>// element registrationPolymer({is: "element-name",// add properties and methods on the element's prototypeproperties: {// declare properties for the element's public APIgreeting: {type: String,value: "Hello!"}}}); </script>像普通標簽一樣使用
<element-name></element-name> <!-- <div>hello!</div> -->注冊和生命周期
注冊自定義元素
使用polymer注冊一個元素,使用is屬性指明要注冊元素的名稱
// register an element MyElement = Polymer({is: 'my-element',// See below for lifecycle callbackscreated: function() {this.innerHTML = 'My element!';}});// create an instance with createElement: var el1 = document.createElement('my-element');// ... or with the constructor: var el2 = new MyElement();polymer function 將元素注冊到瀏覽器上 并且 返回一個創(chuàng)建新實例的元素構(gòu)造函數(shù)
定義一個自定義構(gòu)造函數(shù)
polymer function返回一個基本的構(gòu)造函數(shù),可用于實例化自定義元素,如果你想要向構(gòu)造函數(shù)傳遞參數(shù)配置新元素,您可以指定一個自定義構(gòu)造函數(shù)原型。
MyElement = Polymer({is: 'my-element',constructor: function(foo, bar) {this.foo = foo;this.configureWithBar(bar);},configureWithBar: function(bar) {...}});var el = new MyElement(42, 'octopus');自定義函數(shù)只當使用構(gòu)造函數(shù)時調(diào)用,作為html標記解析時不調(diào)用
自定義函數(shù)在元素初始化后調(diào)用
擴展html元素
MyInput = Polymer({is: 'my-input',extends: 'input',created: function() {this.style.border = '1px solid red';}});var el1 = new MyInput(); console.log(el1 instanceof HTMLInputElement); // truevar el2 = document.createElement('input', 'my-input'); console.log(el2 instanceof HTMLInputElement); // true回調(diào)生命周期
MyElement = Polymer({is: 'my-element',created: function() {console.log(this.localName + '#' + this.id + ' was created');},attached: function() {console.log(this.localName + '#' + this.id + ' was attached');},detached: function() {console.log(this.localName + '#' + this.id + ' was detached');},attributeChanged: function(name, type) {console.log(this.localName + '#' + this.id + ' attribute ' + name +' was changed to ' + this.getAttribute(name));}});準備回調(diào)和元素初始化
ready: function() {<!-- access a local DOM element by ID using this.$ -->this.$.header.textContent = 'Hello!'; }元素初始化順序
created callback
local DOM constructed
default values set
ready callback
custom constructor (if any)
attached callback
注冊回調(diào)
Polymer.Base also implements registerCallback, which is called by Polymer() to allow Polymer.Base to supply a layering system for Polymer features.
標簽靜態(tài)屬性
如果一個自定義標簽需要標簽上出現(xiàn)attributes要在hostAttrbuites下寫 值為true會被轉(zhuǎn)變?yōu)榭?br />false 該屬性不會添加
mixins屬性
fun-mixin.html
FunMixin = {funCreatedCallback: function() {this.makeElementFun();},makeElementFun: function() {this.style.border = 'border: 20px dotted fuchsia;';}};});my-element.html
<link rel="import" href="fun-mixin.html"><script>Polymer({is: 'my-element',mixins: [FunMixin],created: function() {this.funCreatedCallback();}}); </script>類樣式的構(gòu)造函數(shù)
如果你想實現(xiàn)你的元素,但并不注冊他,你可以使用Polymer.class function Polymer.class和Polymer有著相同的屬性配置,設(shè)置原型鏈,沒有注冊元素,可以用document.registerElement 注冊。
申明屬性
你可以在你的元素上聲明有哪些properties通過在polymer構(gòu)造函數(shù)原型properties寫
可以聲明那些配置
屬性類型
默認值
屬性改變觀察者
只讀
出發(fā)事件
基于別的屬性計算屬性
屬性值改變時跟新相關(guān)
| type | (Boolean,Date,Number,String,Array,Object) |
| value | (Boolean,Number,String,Function) 默認值 |
| reflectToAttribute | (Boolean) |
| readyOnly | (Boolean) |
| notify | (Boolean) |
| computed | (String) |
| observer | (String) 屬性觀察者函數(shù)名 |
property name 和 attribute name 映射
當映射 attribute name 到 property names
attribute names 轉(zhuǎn)換成 小寫 property names 比如firstName 映射成 firstname
attribute names 帶破折號 轉(zhuǎn)換成 駝峰命名 property namses 比如first-name 映射成
firstName
property names 映射成 attribute names時一致
反序列化屬性
屬性最好設(shè)置type
<script>Polymer({is: 'x-custom',properties: {user: String,manager: {type: Boolean,notify: true}},attached: function() {// renderthis.innerHTML = 'Hello World, my user is ' + (this.user || 'nobody') + '.\n' +'This user is ' + (this.manager ? '' : 'not') + ' a manager.';}});</script><x-custom user="Scott" manager></x-custom> <!-- <x-custom>'s innerHTML becomes: Hello World, my user is Scott. This user is a manager. -->attributes dash-case 風格 轉(zhuǎn)換成 camel-case 風格
<script>Polymer({is: 'x-custom',properties: {userName: String}});</script><x-custom user-name="Scott"></x-custom> <!-- Sets <x-custom>.userName = 'Scott'; -->配置默認屬性值
properties 的默認值可以再properties對象使用value屬性 可以是一個原始值或者一個function的返回值
Polymer({is: 'x-custom',properties: {mode: {type: String,value: 'auto'},data: {type: Object,notify: true,value: function() { return {}; }}}});屬性更改回調(diào)(觀察者)
Polymer({is: 'x-custom',properties: {disabled: {type: Boolean,observer: 'disabledChanged'},highlight: {observer: 'highlightChanged'}},disabledChanged: function(newValue, oldValue) {this.toggleClass('disabled', newValue);this.highlight = true;},highlightChanged: function() {this.classList.add('highlight');setTimeout(function() {this.classList.remove('highlight');}, 300);}});觀察多個屬性更改
Polymer({is: 'x-custom',properties: {preload: Boolean,src: String,size: String},observers: {'preload src size': 'updateImage'},updateImage: function(preload, src, size) {// ... do work using dependent values}});觀察更改子屬性
local Dom
我們叫把可以創(chuàng)造和管理的dom叫l(wèi)ocal dom
polymer支持創(chuàng)建multiple local dom 在支持shadow dom的瀏覽器上 shadow dom用來創(chuàng)建local dom
在其他瀏覽器 polymer通過自定義實現(xiàn)的shadow dom提供local dom
local dom template
使用<dom-module>元素表現(xiàn)local <dom-module>的id元素對應(yīng)元素 is property在dom-module內(nèi) 放置<template> polymer會自動拷貝模板內(nèi)容為元素的local dom
<dom-module id="x-foo"><template>I am x-foo!</template> </dom-module><script>Polymer({is: 'x-foo'}); </script>scoped styling
<dom-module id="my-element"><style>:host {display: block;border: 1px solid red;}#child-element {background: yellow;}/* styling elements distributed to content (via ::content) requires *//* using a wrapper element for compatibility with shady DOM */.content-wrapper > ::content .special {background: orange;}</style><template><div id="child-element">In local Dom!</div><div class="content-wrapper"><content></content></div></template></dom-module><script>Polymer({is: 'my-element'});</script>Styling distributed children (::content)
在這個例子,這個規(guī)則
.content-wrapper ::content > .special翻譯為
.content-wrapper > specialAutomatic node finding
內(nèi)部元素使用id聲明 使用this.$ hash選擇
DOM (re-)distribution
總結(jié)
以上是生活随笔為你收集整理的polymer web componets 大前端的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: LL-verilog-HDLBitSim
- 下一篇: HTML5与搜索引擎优化[转载]