在前面的几节里面简单的介绍了一下Polymer的基本功能,但还有一些细节的东西并没有讨论,所有打算花点时间把Polymer的一些细节写一下。

new和createElement有区别吗?
<script>
var SayHello = Polymer({
is:'say-Hello'
})
var el1 = document.createElement('say-Hello');
console.log(el1);
var el2 = new SayHello();
console.log(el2);
</script>

看起来好像没有差别,但还是有些差别的,new的方式可以传递参数。

<script>
var SayHello = Polymer({
is:'say-Hello',
factoryImpl:function(){
console.log(arguments);
}
})
var el = new SayHello('css','js','html');
console.log(el);
</script>

注:factoryImpl:只有使用new ElementClass()方式创建组件时会被调用。更多关于函数的调用时机可以看前端组件化Polymer入门教程(5)——生命周期

属性值还可以通过函数return返回。

例1

<say-Hello></say-Hello>
<dom-module id="say-Hello">
<template>
<h1>{{say}}</h1>
</template>
</dom-module>
<script>
Polymer({
is:'say-Hello',
properties:{
say:{
type:String,
value:function(){
return '国庆七天乐!';
}
}
}
})
</script>

例2

<say-Hello></say-Hello>
<dom-module id="say-Hello">
<template>
<h1>{{say.title}}</h1>
</template>
</dom-module>
<script>
Polymer({
is:'say-Hello',
properties:{
say:{
type:Object,
value:function(){
return {"title":"这是一段标题"};
}
}
}
})
</script>

异步方法async
<say-Hello></say-Hello>
<dom-module id="say-Hello">
<template>
<h1>{{say}}</h1>
</template>
</dom-module>
<script>
Polymer({
is:'say-Hello',
properties:{
say:{
type:String,
value:'hello',
observer:'attrChange'
}
},
listeners:{
'click':'setAttr'
},
setAttr:function(){
this.async(function(){
console.log('async ' + this.say);
},300);
this.say = 'attr';
},
attrChange:function(){
console.log('属性值已改成' + this.say);
}
})
</script>

this.async和setTimeout使用差不多。

this.push(name,value)和push(value)的区别
<x-custom></x-custom>
<dom-module id="x-custom">
<template>
<ul>
<template is="dom-repeat" items="{{users}}
<li>{{item.userName}}</li>
</template>
</ul>
</template>
</dom-module>
<script>
Polymer({
is: 'x-custom',
ready: function() {
this.users = [
{userName:'老王'}
];
},
listeners:{
'click':'addUser'
},
addUser:function(){
this.push('users',{userName:'老李'})
this.users.push({userName:'老于'});
console.log(this.users);
}
});
</script>

区别在于一个会更新视图,一个不会更新。

计算功能

可以通过computed来设置计算功能

<x-custom first="国庆" last="快乐"></x-custom>
<dom-module id="x-custom">
<template>
My name is <span>{{fullName}}</span>
</template>
<script>
Polymer({
is: 'x-custom',
properties: {
first: String,
last: String,
fullName: {
type: String,
computed: 'computeFullName(first, last)'
}
},
computeFullName: function(first, last) {
return first + ' ' + last;
},
listeners:{
'click':'changeFullName'
},
changeFullName:function(){
this.first = 'Hello';
this.last = 'World';
}
});
</script>
</dom-module>

注意只有当first和last都改变的时候才会执行computed里面的方法。

前端组件化Polymer深入篇(1)的更多相关文章

  1. 前端组件化Polymer入门教程(1)——初识&&安装

    前端组件化Polymer入门教程目录: 前端组件化Polymer入门教程(1)--初识&&安装 前端组件化Polymer入门教程(2)--快速入门 前端组件化Polymer入门教程(3 ...

  2. 前端组件化Polymer入门教程(5)——生命周期

    以前我对生命周期这个概念还真不是很清楚,不过想想也简单,比如说人的生命周期,无非就是生老病死.而对于程序的生命周期就是说,它在每个阶段都会做不同的事,再比如说回调函数把,ajax返回的时候它才执行,那 ...

  3. 前端组件化Polymer入门教程(3)——快速入门

    本系列主要翻译官方的教程,因为国内目前这方面的资料太少了,但也不一定和官网的一样,反正就是自己想到哪就写到哪. 如果我没有说明,默认情况下index.html始终包含这段代码,后面将不会再贴上来. & ...

  4. 前端组件化Polymer入门教程(2)——Hello world

    本节为体验篇,就是让你了解它有哪些功能,不做详细说明,后面再来讲细节. 自定义元素 组件页 <link rel="import" href="../polymer- ...

  5. 前端组件化Polymer入门教程(4)——自定义元素

    除了上一篇说到的创建自定义元素方法以外,还可以通过原生JS来创建,当你需要动态的创建元素时可以通过这种方式. template.html <link rel="import" ...

  6. 前端组件化Polymer入门教程(8)——事件

    可以在listeners对象中监听事件 <x-custom></x-custom> <dom-module id="x-custom"> < ...

  7. 前端组件化Polymer入门教程(7)——Local DOM

    DOM元素的创建和管理被称为本地DOM(Local DOM) 本地DOM模板 如果你需要使用本地DOM,你们需要用<dom-module>并指定一个相匹配的ID. <dom-modu ...

  8. 前端组件化Polymer入门教程(6)——监听属性值变化

    监听属性值变化 如果需要监听属性值变化可以通过给observer赋值一个回调函数. <say-Hello></say-Hello> <dom-module id=&quo ...

  9. Vue.js:轻量高效的前端组件化方案

    转发一篇尤老师对vue.js的介绍,了解vue.js的来龙去脉.不过现在已经是2.0了,也有添加一些新的东西,当然有些东西也改了. Vue.js:轻量高效的前端组件化方案 Vue.js 是我在2014 ...

随机推荐

  1. Shell编程-11-子Shell和Shell嵌套

    目录 什么是子Shell 子Shell产生的途径 Shell脚本调用模式 什么是子Shell     子Shell的概念其实是贯穿整个Shell的,如果想要更好的理解和写Shell脚本则必须要了解子S ...

  2. jsp 中出现大量红线,而且页面能正常访问

    第一次,出现这种情况真的很苦恼,估计是有强迫症的原因,就是看着不舒服,都页面能正常访问,但是还是想解决它 解决方法:依次按下 ctl+A ctl+X.ctl+V, 没看错就是 全选,剪切,粘贴 就好了 ...

  3. 在CentOS上安装GITLAB

    为什么要用gitlab? 方便地管理项目,设置用户权限. 参考 https://gitlab.com/gitlab-org/omnibus-gitlab/blob/master/README.md 步 ...

  4. htpasswd建立和更新存储用户名、密码

    htpasswd建立和更新存储用户名.密码的文本文件, 用于对HTTP用户的basic认证. # /usr/local/apache/bin/htpasswd --help Usage: htpass ...

  5. 不合法的DB Index

    redis报错,Invalid Db Index . 需要清理一下redis, 进入redis文件加下,登录redis, redis-cli ,输入账号密码, flush all, 回收程序池,重新生 ...

  6. c# List使用中遇到的问题

    最近在项目上写的方法,想通过减少访问数据层,将需要重复调用的值存入List,无意中碰到的一个巨坑,至今仍不明所以,在此写出来,一来是看看有没有同道中人,二来是看看有没有大牛能解惑. 逻辑如下: 1.从 ...

  7. 自定义延时关闭弹窗,替代MesssageBox

    1,新建一个窗体MessageForm,在里面加一个label控件和timer 2,代码如下: public partial class MessageForm : Form { int t; str ...

  8. WPF学习笔记(2):准确定位弹出窗

    效果图:使弹出的列表框紧随在单元格的下边缘. 第一次,尝试在XAML中设置Popup的定位方式:Placement="Mouse".基本能够定位,但当在输入前移动鼠标,列表框就会随 ...

  9. Spring IOC 容器源码分析 - 创建单例 bean 的过程

    1. 简介 在上一篇文章中,我比较详细的分析了获取 bean 的方法,也就是getBean(String)的实现逻辑.对于已实例化好的单例 bean,getBean(String) 方法并不会再一次去 ...

  10. Sublime Text3 实现在浏览器中以HTML格式预览md文件

    1.首先找到Package Control 打开Sublime Text3,找到菜单栏:Preferences → Package Control,没有找到Package Control,那么点击Pa ...