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

时间: 2024-08-09 18:34:22

5.5 Components -- Customizing A Compnent's Element的相关文章

Knockoutjs-关于components

components可以作为widgets来呈现,它包含了自己独立的view,能够被预加载或者通过异步的方式加载,能够接受参数并返回结果,可继承于其他组件,也可被重用. 通过使用components,我们可以使用自定义的语义化标签来代替<div>,这种方式在旧的浏览器下同样可以实现兼容. 一个组件中包含了viewmodel和template,所以在使用的时候需要像这样的方式来注册组件: 1 ko.components.register( 'component-name' , { 2 viewM

JAXB - The Object Factory

Usually hidden in the middle of the list of the classes derived from the types defined in an XML schema there will be one class called ObjectFactory. It's convenient to use the methods of this class because they provide an easy way of creating elemen

jquery easyui 1.4.1 API( CHM版)

ChangeLog Bug The combogrid has different height than other combo components. fixed. datagrid: The row element loses some class style value after calling 'updateRow' method. fixed. menubutton: Calling 'enable' method on a disabled button can not work

ReactJs 报错 Element type is invalid: expected a string (from built-in components) or a class/function (for composite components) but got: undefined. Check the render method of `Me`.

今天在重构一个页面的时候,碰到了一个error,具体的error信息如下图中所示: 最后经过一番查找定位,终于找到了问题所在,原因就是在父组件引用子组件时多加了一个大括号. import {Chart} from 'Chart'; 把{}去掉就可以了.

React报错:Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined.

React报错: 报错原因List.Item的Item写成了item,小写的i 原文地址:https://www.cnblogs.com/nayek/p/12370052.html

【ExtJS 4.x学习教程】(4)组件(Components)

作者:周邦涛(Timen) Email:[email protected] 转载请注明出处:  http://blog.csdn.net/zhoubangtao/article/details/27366477 1. 简介 一个Ext JS 应用的UI是由一个或多个叫做组件(Component)的小部件组成的.所有的组件都是Ext.Component的子类,Ext.Component可以使其子类参与自动化的声明周期管理,包括初始化.渲染.调整大小及位置和销毁.Ext JS提供了大量的直接可用的组

[React] Recompose: Theme React Components Live with Context

SASS Bootstrap allows us to configure theme or branding variables that affect all components (e.g. Primary Color or Link Color). When we isolate our styles inside React components we lose the ability to theme them. To get round this we can put our th

Learning OpenCV Lecture 6 (Extracting Lines,Contours, and Components)

In this chapter, we will cover: Detecting image contours with the Canny operator Detecting lines in images with the Hough transform Fitting a line to a set of points Extracting the components' contours Computing components' shape descriptors Detectin

Customizing and Overriding User Login page, Register, and Password Reset in Drupal 6 and 7

Customizing the user login, register, and password reset pages is fairly simple, and uses the following concepts: preprocessing to set variables registration of functions in the theme registry creation of one or more theme templates. Step 1.In the si