一、概述

默认的,每一个组件都基于一个<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的更多相关文章

  1. Ember.js学习教程 -- 目录

    写在前面的话: 公司的新项目需要用到Ember.js,版本为v1.13.0.由于网上关于Ember的资料非常少,所以只有硬着头皮看官网的Guides,为了加深印象和方便以后查阅就用自己拙劣的英语水平把 ...

  2. 5.2 Components — Defining A Component

    一.概述 1. 为了定义一个组件,创建一个模板,它的名字以components/开头.为了定义一个新组件{{blog-post}},例如,创建一个components/blog-post模板. 2.注 ...

  3. (八)Knockout 组件 Components

    概述 :组件和自定义元素 Components 是将UI代码组织成自包含的.可重用的块的一种强大而干净的方法.他们: -可以表示单个控件/窗口小部件或应用程序的整个部分 -包含它们自己的视图,并且通常 ...

  4. 七、Vue组件库:Element、Swiper(轮播专用组件)

    一.vue的Element组件库 官网:https://element.eleme.cn/#/zh-CN 1.1安装 推荐安装方法: 首先要进入项目目录 cnpm i element-ui -S 或 ...

  5. [asp.net core] Tag Helpers 简介(转)

    原文地址 https://docs.microsoft.com/en-us/aspnet/core/mvc/views/tag-helpers/intro What are Tag Helpers? ...

  6. KnockoutJS 3.X API 第六章 组件(4) 自定义元素

    自定义元素提供了一种将组件注入视图的方便方法. 本节目录 介绍 例子 传递参数 父组件和子组件之间的通信 传递监控属性的表达式 将标记传递到组件中 控制自定义元素标记名称 注册自定义元素 备注1:将自 ...

  7. jquery easyui 1.4.1 API( CHM版)

    ChangeLog Bug The combogrid has different height than other combo components. fixed. datagrid: The r ...

  8. 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 ...

  9. JAXB - The Object Factory

    Usually hidden in the middle of the list of the classes derived from the types defined in an XML sch ...

随机推荐

  1. oracle 日期函数 求年的最后一天、第一天,月的最后一天

    add_months(trunc(to_date('2013','yyyy') ,'yyyy'),12)-1  2013年最后一天 trunc(to_date('2013','yyyy') ,'yyy ...

  2. Extjs学习笔记--(四,基本函数介绍)

    Ext是Extjs的命名空间,为Extjs框架提供唯一的全局变量 这样做可以避免冲突,便于代码维护 1,apply和applyif方法 apply=function(object, config, d ...

  3. swift - UIAlertView 的用法

    1,创建一个alertview,并带有“确定”和“取消”两个按钮 (注:在这里使用alertview,会报警告,那是因为从ios 8 以后,建议使用UIAlertviewController) //警 ...

  4. eclipse中tomcat能正常启动,在浏览器中不能打开问题

    问题原因:没有在eclipse中tomcat的server location设置到tomcat的安装目录. 解决办法:1.选择server点击右键,选择Open选项,然后在server locatio ...

  5. js常用总结

    常用总结,方便大家学习共享.   1.document.write(""); 输出语句 2.JS中的注释为// 3.传统的HTML文档顺序是:document->html-& ...

  6. IOS视频播放器的制作

    利用自带MPMoviePlayerController来实现视频播放,首先要在项目中导入MediaPlayer.Framework框架包. 在视图控制器中 #import "MediaPla ...

  7. TextureMerger1.6.6 一:Egret MovieClip的制作和使用

    本随笔记录TextureMerger来制作动画,并在Egret中使用. 参考官网教程:http://bbs.egret.com/forum.php?mod=viewthread&tid=918 ...

  8. Windows Phone 7 检查手机网络

    using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Wi ...

  9. 手机联系人信息获取(contacts) ---- HTML5+

    模块:contacts Contacts模块管理系统通讯录,用于可对系统通讯录进行增.删.改.查等操作.通过plus.contacts获取系统通讯录管理对象. 对象:联系人对象(属性:电话,地址等)针 ...

  10. SpringMVC中使用@ResponseBody注解将任意POJO对象返回值转换成json进行返回

    @ResponseBody 作用: 该注解用于将Controller的方法返回的对象,通过适当的HttpMessageConverter转换为指定格式后,写入到Response对象的body数据区. ...