5.2 Components — Defining A Component
一、概述
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的更多相关文章
- [Vue @Component] Dynamic Vue.js Components with the component element
You can dynamically switch between components in a template by using the reserved <component> ...
- [Vue] Dynamic Vue.js Components with the component element
You can dynamically switch between components in a template by using the reserved <component> ...
- angularjs component
Component https://docs.angularjs.org/guide/component component本质上就是directive. This is a shorthand fo ...
- [zz] Principal Components Analysis (PCA) 主成分分析
我理解PCA应该分为2个过程:1.求出降维矩阵:2.利用得到的降维矩阵,对数据/特征做降维. 这里分成了两篇博客,来做总结. http://matlabdatamining.blogspot.com/ ...
- Exploring the Angular 1.5 .component() method
Angular 1.5 introduced the .component() helper method, which is much simpler than the.directive() de ...
- [React] Styling React Components With Aphrodite
Aphrodite is a library styling React components. You get all the benefits of inline styles (encapsul ...
- [React] Higher Order Components (replaces Mixins)
Higher order components will allow you to apply behaviors to multiple React components. So the idea ...
- [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 ...
- angular2 学习笔记 ( Component 组件)
refer : https://angular.cn/docs/ts/latest/guide/template-syntax.html https://angular.cn/docs/ts/late ...
随机推荐
- 如何调用别人发布的WebService程序
这篇经验会告诉我们如何调用别人发布的WebService,并且需要注意的事项.现在就拿获取天气预报的接口举例,因为文中不允许有链接,所以在下文图中有WebService链接的地址. 工具/原料 V ...
- C语言函数參数传递原理
C语言中參数的传递方式一般存在两种方式:一种是通过栈的形式传递.还有一种是通过寄存器的方式传递的. 这次.我们仅仅是具体描写叙述一下第一种參数传递方式,第二种方式在这里不做具体介绍. 首先,我们看一下 ...
- [转载]SQL truncate 、delete与drop区别
相同点: 1.truncate和不带where子句的delete.以及drop都会删除表内的数据. 2.drop.truncate都是DDL语句(数据定义语言),执行后会自动提交. 不同点: 1. t ...
- poj_1190 树状数组
题目大意 给定一个S*S的矩形,该矩形由S*S个1x1的单元格构成,每个单元格内可以放一个整数,每次有如下可能操作: (1)改变某个单位单元格中的数的大小 (2)查询由若干个连续单元格构成的X*Y的大 ...
- VIM 插入
不知道有多少VIM新手和我当年(去年)一样,信誓旦旦的以为只有i可以插入 唉,现在想想都觉得可笑,都是Windows下的编辑器用多了的结果 鼠标一点,妈妈再也不用担心我的文本插入了……悲剧! 好了,让 ...
- Neutron SDN 手动实现手册
安装架构介绍 本文旨在通过自己搭建类似neutron (openvswitch + gre) 实现SDN 的环境,学习了解其工作原理,模拟核心原理,比如:同一租户自定义网络 instance 互通,手 ...
- web移动端一些常用知识
1.去掉 a,input 在移动端浏览器中的默认样式(半透明灰色遮罩阴影) a,button,input,optgroup,select,textarea { -webkit-tap-highligh ...
- Mac - 苹果电脑mac系统释放硬盘空间方法汇总
硬盘空间是大家最头痛的一个问题,大家在硬盘空间变小的时候怎么腾空间的呢?下面为大家分享7个mac系统释放空间的高级方法,大家赶紧来收了! mac系统释放硬盘空间方法: 方法一:删除Emacs--可以节 ...
- (贪心)kruskal思想
hdu4313 Matrix Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- Python 使用 Matplotlib 做图时,如何画竖直和水平的分割线或者点画线或者直线?
作者:看看链接:https://www.zhihu.com/question/21929761/answer/164975814 可以使用: vlines(x, ymin, ymax) hlines( ...