一、概述

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

2.注意:在组件的名字中至少要有一个破折号。所以blog-post是可以接受的名字,audio-player-controls也是,但是post不是。这样可以防止与当前或者未来的HTML元素名字冲突,并且确保Ember自动检测组件。

3. 一个组件的例子:

app/templates/components/blog-post.hbs

<h1>Blog Post</h1>
<p>Lorem ipsum dolor sit amet.</p>

4. 名字以components/开始的模板创建一个和它同名的组件。假设是上面的模板,你现在可以使用{{blog-post}}自定义元素:

app/templates/index.hbs

{{#each model as |post|}}
{{#blog-post title=post.title}}
{{post.body}}
{{/blog-post}}
{{/each}}

app/templates/components/blog-post.hbs

<article class="blog-post">
<h1>{{title}}</h1>
<p>{{yield}}</p>
<p>Edit title: {{input type="text" value=title}}</p>
</article>

app/routes/index.js

var posts = [{
title: "Rails is omakase",
body: "There are lots of à la carte software environments in this world."
}, {
title: "Broken Promises",
body: "James Coglan wrote a lengthy article about Promises in node.js."
}]; export default Ember.Route.extend({
model() {
return posts;
}
});

app/components/blog-post.js

export default Ember.Component.extend({
});

4. 每一个组件,背地里,是被一个元素支持的。默认的Ember将会使用<div>元素去包含你的组件模板。为了学习Ember使用你的模板如何改变元素,请看Customizing a Component's Element

二、Defining a component subclass

1. 多数情况下,你的组件将只是封装特定片段的Handlebars templates,你一遍又一遍的使用。在这种情况下,写任何JS代码。就像上面描述的定义Handlebars template并且使用被创建的组件。

2. 如果你需要自定义组件的行为,你需要定义一个Ember.Component的子类。例如,如果你想改变一个组件的元素你将需要一个自定义的子类,响应来自组件模板的actions,或者使用JS手动更改组件的元素。

3. Ember基于它的文件名知道那个子类驱动组件。例如,如果你有一个名为blog-post的组件,你将创建一个文件app/components/blog-post.js。如果你的组件名为audio-player-controls,文件名将会是app/components/audio-player-controls.js

三、Dynamically rendering a component

1. {{component}}辅助器可以被用来推迟一个组件的选择到运行时。{{my-component}}句法将总是加载相同的模板,而使用 {{component}}辅助器允许在动态加载时交换组件。在一些情况下这是有用的,你希望和基于数据的不同的外部库交互。使用{{component}}辅助器允许你保留这些不同的逻辑良好分离。

2. 辅助器的第一个参数是要渲染的组件名字,是一个字符串。所以如果你有{{component 'blog-post'}},这和{{blog-post}}是一样的。

3. {{component}}的真正值来自能够动态挑选被加载的组件。下面是一个使用辅助器的例子,为展现不同类型的posts派遣不同的组件:

app/templates/components/foo-component.hbs

<h3>Hello from foo!</h3>
<p>{{post.body}}</p>

app/templates/components/bar-component.hbs

<h3>Hello from bar!</h3>
<div>{{post.author}}</div>

app/routes/index.js

var posts = [{
componentName: 'foo-component', // key used to determine the rendered component
body: "There are lots of à la carte software environments in this world."
}, {
componentName: 'bar-component',
author: "Drew Crawford"
}]; export default Ember.Route.extend({
model() {
return posts;
}
});

app/templates/index.hbs

{{#each model as |post|}}
{{!-- either foo-component or bar-component --}}
{{component post.componentName post=post}}
{{/each}}

4. 为了简便起见,componentName被硬编码进每一个post中,它可以很好地作为一个计算属性,可以减少基于数据的目标组件。

5. 当传递给{{component}}的参数等价于null或者undefined时,辅助器什么都不会加载。当参数变化时,当前加载的组件会被销毁并且新的组件被创建并且引入。

6. 响应数据挑选不同的组件去加载可以让你为每一种情况有不同的模板和行为。该{{component}}助手是一个功能强大的工具,提高代码的模块化。

5.2 Components — Defining A Component的更多相关文章

  1. [Vue @Component] Dynamic Vue.js Components with the component element

    You can dynamically switch between components in a template by using the reserved <component>  ...

  2. [Vue] Dynamic Vue.js Components with the component element

    You can dynamically switch between components in a template by using the reserved <component>  ...

  3. angularjs component

    Component https://docs.angularjs.org/guide/component component本质上就是directive. This is a shorthand fo ...

  4. [zz] Principal Components Analysis (PCA) 主成分分析

    我理解PCA应该分为2个过程:1.求出降维矩阵:2.利用得到的降维矩阵,对数据/特征做降维. 这里分成了两篇博客,来做总结. http://matlabdatamining.blogspot.com/ ...

  5. Exploring the Angular 1.5 .component() method

    Angular 1.5 introduced the .component() helper method, which is much simpler than the.directive() de ...

  6. [React] Styling React Components With Aphrodite

    Aphrodite is a library styling React components. You get all the benefits of inline styles (encapsul ...

  7. [React] Higher Order Components (replaces Mixins)

    Higher order components will allow you to apply behaviors to multiple React components. So the idea ...

  8. [React Native] Using the Image component and reusable styles

    Let's take a look at the basics of using React Native's Image component, as well as adding some reus ...

  9. angular2 学习笔记 ( Component 组件)

    refer : https://angular.cn/docs/ts/latest/guide/template-syntax.html https://angular.cn/docs/ts/late ...

随机推荐

  1. Cocos2d-x 3.0final 终结者系列教程10-画图节点Node中的Action

    Action是作用在Node上的逻辑处理,比方让Node移动.旋转.缩放.变色.跳跃.翻转.透明等等.都有相相应的Action Action怎样在Node上使用 1. 定义Action对象 如 aut ...

  2. python2.0_day19_前端分页功能的实现

    我们前面完成的客户纪录展示,只有4条,如果有上百条就不能在1页中全部展示了,那样是不人性化的.另外一次性取出来,数据量也比较大.假如现在有95条数据,我们想实现一个每页展示20条,那就分为5页.假如我 ...

  3. 用代码走进Ftp

    因为最近做一个关于集中采集的ftp改造开发.所以研究了哈ftp的开发. 一个简单常用的连接ftp的命令:ftp 主机ip 下面贴出我自己的ftp的demo. 1.FtpUtil工具类 import j ...

  4. Keil(MDK-ARM)在线调试(Ⅰ)(转)

    Ⅰ.写在前面 Keil在线调试的内容有很多,本文带来在线调试常用的内容:Debug Toolbar调试工具栏(复位.全速运行.停止运行.单步调试.逐行调试.跳出调试.运行到光标行.跳转到暂停行.调试窗 ...

  5. MUI 单个图片上传预览(拍照+系统相册):先选择->预览->上传提交

    1 html部分 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> < ...

  6. css3-巧用选择器 “:target”

    今天(昨天)又发现一个知识盲区 css3的:target标签,之前学习的时候就是一眼扫过,说是认识了,但其实也就记了三分钟,合上书就全忘光了. 直到昨天,遇到一个有意思的题目,用css3新特性做一个类 ...

  7. python实现HTTP代理的思路和Demo

    一.首先什么是代理: 代理其实就是中间转发的那个玩意,所以在代码逻辑上也是如此的. 二.Python写http代理的基本逻辑: (1)接受浏览器发出的请求,解析,拼凑成该有的样子,然后使用套接字发出去 ...

  8. 微信小程序 --- Image组件

    Image组件可以在小程序中展示图片,支持外链. Image组件可以调用API,进行三种缩放,九种裁剪. Image组件有默认值:300*225 属性: src:图片资源地址. mode:图片裁剪缩放 ...

  9. Oracle数据类型number

    oracle数值类型只有number number(变长) 1.number可以存放整数,可以存放小数: 2.number(p,s) 说明: p表示有效位,s为小数位:范围p[1,38],s[-84, ...

  10. 【ArcGIS for JavaScript api】Clusterlayer聚簇类

    1.作用: 聚簇类是用于前端显示优化,使POI点要素显示更为美观.大量的Marker距离太近会引起压盖而对浏览或者操作产生不便,因此,一般在超过1K点的时候,用此类.. 2.使用方式: 1: // c ...