5.5 Components -- Customizing A Compnent's Element
一、概述
默认的,每一个组件都基于一个<div>元素。如果你在开发者工具中查看一个渲染的组件,你将会看到一个像这样的DOM表示:
<div id="ember180" class="ember-view">
<h1>My Component</h1>
</div>
你可以为你的组件自定义Ember生成元素的类型,包括它的属性和类名,通过创建一个Ember.Component的子类。
二、Customizing the element
使用一个不是div的标签,Ember.Component的子类分配给它一个tagName属性。这个属性可以以任何有效的HTML5标签名称作为字符串。
app/components/navigation-bar.js
export default Ember.Component.extend({
tagName: 'nav'
});
app/templates/components/navigation-bar.hbs
<ul>
<li>{{#link-to 'home'}}Home{{/link-to}}</li>
<li>{{#link-to 'about'}}About{{/link-to}}</li>
</ul>
三、Customizing class names
1. 你也可以通过设置它的classNames属性为一个字符串数组来制定组件的元素的类名。
app/components/navigation-bar.js
export default Ember.Component.extend({
classNames: ['primary']
});
2. 如果你希望通过组件类型来确定类名,你可以使用类名绑定。如果你绑定一个布尔属性,类名将会根据这个值被添加或者被移除:
app/components/todo-item.js
export default Ember.Component.extend({
classNameBindings: ['isUrgent'],
isUrgent: true
});
这个组件将会被渲染为:
<div class="ember-view is-urgent"></div>
如果isUrgent被改为false,然后is-urgent类名会被移除。
3. 默认的,布尔属性的名字被dasherized。你可以通过一个":"来自定义类名:
app/components/todo-item.js
export default Ember.Component.extend({
classNameBindings: ['isUrgent:urgent'],
isUrgent: true
});
者将被渲染为:
<div class="ember-view urgent">
4. 除了该值为true时自定义名称之外,你也可以在该值为false时指定一个类名:
app/components/todo-item.js
export default Ember.Component.extend({
classNameBindings: ['isEnabled:enabled:disabled'],
isEnabled: false
});
这将被渲染为:
<div class="ember-view disabled">
5. 你也可以指定一个类,它仅仅在值为false时被添加:
app/components/todo-item.js
export default Ember.Component.extend({
classNameBindings: ['isEnabled::disabled'],
isEnabled: false
});
将被渲染为:
<div class="ember-view disabled">
如果isEnabled属性被设置为true,不会添加类名:
<div class="ember-view">
6. 如果被绑定的属性值是一个字符串,这个值将会作为属性名被添加不会有任何更改:
app/components/todo-item.js
export default Ember.Component.extend({
classNameBindings: ['priority'],
priority: 'highestPriority'
});
渲染为:
<div class="ember-view highestPriority">
四、Customizing Attriburtes
你可以绑定属性到代表组件的DOM元素,利用attributeBindings:
app/components/link-item.js
export default Ember.Component.extend({
tagName: 'a',
attributeBindings: ['href'],
href: "http://emberjs.com"
});
你也可以绑定这些属性attributes到不同的命名属性properties:
app/components/link-item.js
export default Ember.Component.extend({
tagName: 'a',
attributeBindings: ['customHref:href'],
customHref: "http://emberjs.com"
});
5.5 Components -- Customizing A Compnent's Element的更多相关文章
- Ember.js学习教程 -- 目录
写在前面的话: 公司的新项目需要用到Ember.js,版本为v1.13.0.由于网上关于Ember的资料非常少,所以只有硬着头皮看官网的Guides,为了加深印象和方便以后查阅就用自己拙劣的英语水平把 ...
- 5.2 Components — Defining A Component
一.概述 1. 为了定义一个组件,创建一个模板,它的名字以components/开头.为了定义一个新组件{{blog-post}},例如,创建一个components/blog-post模板. 2.注 ...
- (八)Knockout 组件 Components
概述 :组件和自定义元素 Components 是将UI代码组织成自包含的.可重用的块的一种强大而干净的方法.他们: -可以表示单个控件/窗口小部件或应用程序的整个部分 -包含它们自己的视图,并且通常 ...
- 七、Vue组件库:Element、Swiper(轮播专用组件)
一.vue的Element组件库 官网:https://element.eleme.cn/#/zh-CN 1.1安装 推荐安装方法: 首先要进入项目目录 cnpm i element-ui -S 或 ...
- [asp.net core] Tag Helpers 简介(转)
原文地址 https://docs.microsoft.com/en-us/aspnet/core/mvc/views/tag-helpers/intro What are Tag Helpers? ...
- KnockoutJS 3.X API 第六章 组件(4) 自定义元素
自定义元素提供了一种将组件注入视图的方便方法. 本节目录 介绍 例子 传递参数 父组件和子组件之间的通信 传递监控属性的表达式 将标记传递到组件中 控制自定义元素标记名称 注册自定义元素 备注1:将自 ...
- jquery easyui 1.4.1 API( CHM版)
ChangeLog Bug The combogrid has different height than other combo components. fixed. datagrid: The r ...
- Tomcat Clustering - A Step By Step Guide --转载
Tomcat Clustering - A Step By Step Guide Apache Tomcat is a great performer on its own, but if you'r ...
- JAXB - The Object Factory
Usually hidden in the middle of the list of the classes derived from the types defined in an XML sch ...
随机推荐
- JavaScript------生成Guid方法
转载: http://blog.csdn.net/limm33/article/details/51536529 代码: function newGuid() { var guid = "& ...
- resolution will not be reattempted until the update interval of nexus has elapsed or updates are forced
Maven在执行中报错: - Failure to transfer org.slf4j:slf4j-api:jar:1.7.24 from http://localhost:8081/nexus/c ...
- Linux 下安装 Python3
Linux CentOS 7 安装 Python3: [root@localhost ~]$ yum install -y epel-release [root@localhost ~]$ yum i ...
- 如何在CLI命令行下运行PHP脚本,同时向PHP脚本传递参数?
<?php/* //命令行输入输出流fwrite(STDOUT,"Enter your name:"); $name = trim(fgets(STDOUT)); fwrit ...
- cocos2d-x游戏引擎核心之十二——3.x新特性
v3.0 亮点 使用 C++(C++11) 的特性取代了 Objective-C 的特性 优化了 Labels 优化了渲染器(比 v2.2 更快) 新的事件分发机制 物理引擎集成 新的 UI 对象 J ...
- mysql的介绍和安装
本节内容 1.数据库介绍 2.mysql管理 3.mysql数据类型 4.常用mysql命令 创建数据库 外键 增删改查表 5.事务 6.索引 1.数据库介绍 什么是数据库? 数据库(Database ...
- poj_3261 后缀数组
题目大意 给出一个数字串,找出其中至少重复K次的最长的子串长度. 题目分析 直接用后缀数组来求解,限制height[i]的长度来对排好序的后缀进行分组(这种方法经常在字符串问题中被使用). 先 ...
- jQuery stop()的用法
1.stop([stopAll], [gotoEnd])方法有两个参数(当然可以不传或直传一个),其中stopAll的意思是清除之后的所有动画.gotoEnd的意思是,执行完当前动画. 2.stopA ...
- getOwnPropertyNames() & keys()
1.getOwnPropertyNames方法可以获得对象的所有属性名,并储存于一个数组当中. keys方法只能获取可遍历的属性名并储存于数组. 2.在完成notepad模块模拟的过程中使用了getO ...
- SpringData JPA查询分页demo
SpringData JPA 的 PagingAndSortingRepository接口已经提供了对分页的支持,查询的时候我们只需要传入一个 org.springframework.data.dom ...