5.7 Components — Sending Actions From Components to Your Application
一、概述
1. 当一个组件在模板中被使用时,它具有发送action到这个模板的controller和routes的能力。当重大事件发生的时候,这些允许组件通知application,比如点击组件一个特殊的元素。
2. 像{{action}}Handlebars辅助器,来自组件的actions首先会去到模板的controller。如果controller没有为这个action实现一个处理程序,它将会冒泡到模板的route,然后上升到路由层次。要知道更多的关于冒泡行为的信息,请看Action Budding。
3. 组件被设计为在你的应用程序的不同部分可重用的。为了达到这个重用性,这是很重要的,当组件被用在模板中时,被你的组件发送的actions可以被定制。
4.换句话说,如果你在编写一个按钮组件,你不想发送一个click action,因为它是含糊不清的并且可能与页面上的其他组件冲突。相反,当你的按钮被点击时,你会希望允许他人使用组件去指定发送哪一个action。
5.幸运的是,组件有一个sendAction()方法,当一个组件被用在一个模板中时,它允许它们发送指定的actions。
二、Sending a primary action
1. 许多组件仅仅发送一类action。例如,一个按钮组件当被点击时可能发送一个action,这个就是primary action。
2. 为了设置一个组件的primary action,在Handlebars中设置它的action属性:
{{my-button action="showUser"}}
这就告诉my-button组件,当触发它的primary action时它应该发送showUser action。
3. 那么你如何触发发送一个组件primary action的操作呢?在相关的事件发生之后,你可以不含参数的调用sendAction()方法:
app/components/my-button.js
export default Ember.Component.extend({
click() {
this.sendAction();
}
});
在上面例子中,当这个组件被点击的时候,my-button组件将会发送showUser action。
三、Sending parameters with an action
1. 你可能希望为route或者controller提供额外的上下文来处理一个action。例如,一个按钮组件可能希望告诉一个controller不仅一条数据被删除了,而且还有其他的。
2. 为了使用primary action发送参数,调用sendAction()附加一个'action'字符串作为第一个参数并且其他多余的参数紧随其后:
this.sendAction('action', param1, param2);
例如,假象我们正在创建一个todo list,允许用户删除一个todo:
app/routes/index.js
export default Ember.Route.extend({
model() {
return {
todos: [{
title: "Learn Ember.js"
}, {
title: "Walk the dog"
}]
};
},
actions: {
deleteTodo(todo) {
var todos = this.modelFor('index').todos;
todos.removeObject(todo);
}
}
});
app/templates/index.hbs
{{#each model.todos as |todo|}}
<p>{{todo.title}} <button {{action "deleteTodo" todo}}>Delete</button></p>
{{/each}}
3. 我们希望更新这个app,所以在真正删除一个todo之间,用户必须确认这就是它们的打算。我们将实现一个组件在完成action之前首先和用户二次检查。
在这个组件中,我们触发primary action,我们将传送一个组件的用户指定的额外参数:
app/components/confirm-button.js
export default Ember.Component.extend({
actions: {
showConfirmation() {
this.toggleProperty('isShowingConfirmation');
},
confirm() {
this.toggleProperty('isShowingConfirmation');
this.sendAction('action', this.get('param'));
}
}
});
app/templates/components/confirm-button.hbs
{{#if isShowingConfirmation}}
<button {{action "confirm"}}>Click again to confirm</button>
{{else}}
<button {{action "showConfirmation"}}>{{title}}</button>
{{/if}}
现在我们可以更新我们最初的模板并且用我们的新组建替换{{action}}。
4. 注意,我们已经通过设置组件的action属性来发送指定的action,并且我们已经通过设置组件的param属性指定了哪一个argument应该被作为参数被传送。
四、Sending multipe actions
1. 根据你的组件的复杂性,你可能需要让用户为你的组件生成的不同的事件指定多个不同的actions。例如,假设你正在编写一个form组件,用户可以提交或者取消。根据用户点击哪一个按钮,你希望发送不同的aciton到你的controller或者route。
2. 你可以通过把事件的名字作为第一个参数传递给sendAction()来指定哪一个action被传送。
{{user-form submit="createUser" cancel="cancelUserCreation"}}
在这种情况下,你可以通过this.sendAction('submit')发送createUser action,或者通过调用this.sendAction('cancel')发送cancelUserCreation action。
五、Actions that aren't specified
如果用户使用你的组件没有为一个特殊的事件指定一个action,调用sendAction()将毫无影响。例如,你定义一个组件当点击的时候触发primary action:
app/components/my-button.js
export default Ember.Component.extend({
click() {
this.sendAction();
}
});
如果用户点击它,使用这个组件不委派一个primary action将会没有任何反应。
{{my-button}}
六、Thinking about component actions
1. 一般情况下,你应该考虑组件的actions作为转换一个primitive event(原始事件)(例如鼠标点击或者一个<audio>元素的暂停事件)为应用程序中有意义的actions。
2. 这允许你的routes和controllers去实现actions处理器,用类似这样的名字deleteTodo或者songDidPause而不是模糊的名字类似clike或者pause,对其他开发者来,当在上下文之外阅读代码,这种名字可能是模糊的。
3. 另一种考虑组件actions的途径是作为你的组件的公共API。考虑组件中的哪一个事件可以在其他应用程序中触发actions,这是其他开发者将使用你的组件的主要途径。一般来说,保持这些事件越通用越好,这会使组件更加灵活和可重用。
5.7 Components — Sending Actions From Components to Your Application的更多相关文章
- 从HTML Components的衰落看Web Components的危机 HTML Components的一些特性 JavaScript什么叫端到端组件 自己对Polymer的意见
http://blog.jobbole.com/77837/ 原文出处: 徐飞(@民工精髓V) 搞前端时间比较长的同学都会知道一个东西,那就是HTC(HTML Components),这个东西名字很现 ...
- Ember.js学习教程 -- 目录
写在前面的话: 公司的新项目需要用到Ember.js,版本为v1.13.0.由于网上关于Ember的资料非常少,所以只有硬着头皮看官网的Guides,为了加深印象和方便以后查阅就用自己拙劣的英语水平把 ...
- [译]View components and Inject in ASP.NET MVC 6
原文:http://www.asp.net/vnext/overview/aspnet-vnext/vc 介绍view components view components (VCs) 类似于part ...
- ExtJS笔记5 Components
参考 :http://blog.csdn.net/zhangxin09/article/details/6914882 An Ext JS application's UI is made up of ...
- The differences between Java EE components and "standard" Java classes
https://docs.oracle.com/javaee/7/tutorial/overview003.htm ava EE components are written in the Java ...
- Spring Auto scanning components
Normally you declare all the beans or components in XML bean configuration file, so that Spring cont ...
- From MSI to WiX, Part 4 - Features and Components by Alex Shevchuk
Following content is directly reprinted from : http://blogs.technet.com/b/alexshev/archive/2008/08/2 ...
- [React] React Router: Named Components
In this lesson we'll learn how to render multiple component children from a single route. Define a n ...
- Principal Components Regression, Pt.1: The Standard Method
In this note, we discuss principal components regression and some of the issues with it: The need fo ...
随机推荐
- haffman哈夫曼编码的实现
<span style="font-size:18px;">/* 1.在一棵二叉树中,我们定义从A节点到B节点所经过的分支序列为从A节点到B节点的路径: 定义从A节点到 ...
- Effective C++ Item 19 Treat class design as type design
Too high class topic for me now ................... ................... ................... fill the ...
- shell基础篇(二)-shell变量
1. 定义变量 1).定义变量时,变量名不加美元符号($),如: var="hello world"2).注意,变量名和等号之间不能有空格,这可能和你熟悉的所有编程语言都不一样.同 ...
- Python 使用正则表达式匹配URL网址
使用正则表达式匹配以 .com 或 .cn 为域名后缀的URL地址 In [1]: import re In [2]: str = "http://www.baidu.com/" ...
- osgEarth中的StringUtils头文件中有很多关于字符串的操作
- 深入浅出Docker(四):Docker的集成测试部署之道
1. 背景 敏捷开发已经流行了很长时间,如今有越来越多的企业开始践行敏捷开发所提倡的以人为中心.迭代.循序渐进的开发理念.在这样的场景下引入Docker技术,首要目的就是使用Docker提供的虚拟化方 ...
- 【Java nio】buffer
package com.slp.nio; import org.junit.Test; import java.nio.ByteBuffer; /** * Created by sanglp on 2 ...
- 动态设置progressBar的进度
progressDrawable = this.getResources().getDrawable(R.drawable.image); progressDrawable.setBounds(mSe ...
- Android软件开发之EditText 详解(八)
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://xys289187120.blog.51cto.com/3361352/65718 ...
- 微信小程序 --- page.json文件
page.json 文件用于配置当前目录.page.json文件里的配置可以修改 app.json 配置里面的 window:不能覆盖app.json文件里面的 tabBar / 网络超时/ debu ...