一、概述

默认的,每一个组件都基于一个<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. 超全面的JavaWeb笔记day14<用户注册登录>

    案例:用户注册登录 要求:3层框架,使用验证码 1 功能分析 l 注册 l 登录 1.1 JSP页面 l regist.jsp Ø 注册表单:用户输入注册信息: Ø 回显错误信息:当注册失败时,显示错 ...

  2. MySQL建表字段类型

    1.数据库:在MySQL中,要存储数据,必须从数据库开始,因此首先要创建数据库,但由于学校的MySQL服务器对学生数据帐号有限止,学生不得创建数据库,故每个学生的帐号中已事先由信息中心为大家建立了一个 ...

  3. Nginx.conf 配置文件详细说明

    在此记录下Nginx服务器nginx.conf的配置文件说明, 部分注释收集与网络. #运行用户user www-data;    #启动进程,通常设置成和cpu的数量相等worker_process ...

  4. php面向对象的简单总结 $this $parent self

    面向对象涉及到的比较多,大概总结整理一下php的属性.对象,以及访问方式$this  $parent  self  的使用场景. 1. PHP类属性定义和访问方式: 1 <?php 2 clas ...

  5. HTTP/2探索第二篇——工具及应用

    版权声明:本文由张浩然原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/88 来源:腾云阁 https://www.qclou ...

  6. 关于记录cookie引发的问题

    很多时候我们会通过记录cookie的方式来记录用户的最后一次行为,但是对cookie的处理是在js中进行的. 但通常情况下,html.css都要早于js加载完成,并且可能在js生效之前就已经渲染完成了 ...

  7. Rsync匿名访问漏洞

    前言 前两天总结了互联网或者说IT公司内网常见的漏洞,然后决定针对还没学习过不了解的漏洞进行学习了解,所以准备一一针对来研习,今天是第一篇,立一个Flag,争取今年搞定,为啥说的这么艰难,因为平时工作 ...

  8. 【BZOJ2588】Spoj 10628. Count on a tree 主席树+LCA

    [BZOJ2588]Spoj 10628. Count on a tree Description 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lasta ...

  9. 【office2010】office2010安装问题的解决方案。

    今天想在公司电脑上按上一个office2010,结果出现一个问题,导致研究了一下午才解决:现总结解决方案: 安装office 2010,提示需要安装MSXML版本6.10.1129.0组件.但是在网上 ...

  10. eclipse 安装Subversion1.82(SVN)插件

    Eclipse下SVN插件的安装,可以选择在线安装和离线安装两种方式: 2.(可选①)使用本地安装包安装插件 --填写插件名(可随意取名) --插件来源地址(①安装包,②使用网址) →Archie→选 ...