一、概述

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的更多相关文章

  1. 从HTML Components的衰落看Web Components的危机 HTML Components的一些特性 JavaScript什么叫端到端组件 自己对Polymer的意见

    http://blog.jobbole.com/77837/ 原文出处: 徐飞(@民工精髓V) 搞前端时间比较长的同学都会知道一个东西,那就是HTC(HTML Components),这个东西名字很现 ...

  2. Ember.js学习教程 -- 目录

    写在前面的话: 公司的新项目需要用到Ember.js,版本为v1.13.0.由于网上关于Ember的资料非常少,所以只有硬着头皮看官网的Guides,为了加深印象和方便以后查阅就用自己拙劣的英语水平把 ...

  3. [译]View components and Inject in ASP.NET MVC 6

    原文:http://www.asp.net/vnext/overview/aspnet-vnext/vc 介绍view components view components (VCs) 类似于part ...

  4. ExtJS笔记5 Components

    参考 :http://blog.csdn.net/zhangxin09/article/details/6914882 An Ext JS application's UI is made up of ...

  5. 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 ...

  6. Spring Auto scanning components

    Normally you declare all the beans or components in XML bean configuration file, so that Spring cont ...

  7. 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 ...

  8. [React] React Router: Named Components

    In this lesson we'll learn how to render multiple component children from a single route. Define a n ...

  9. 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 ...

随机推荐

  1. NHibernate初学五之关联一对多关系

    1:创建两张表T_Country.T_Person:其中T_Person表中有一个CountryID对应T_Country的ID,一个Country可以对应多个Person CREATE TABLE ...

  2. shell基础篇(四)算术运算

    ---内容来源于http://www.jb51.net/article/31232.htm shell中的赋值和操作默认都是字符串处理,1.错误方法举例 a) var=1+1 echo $var 输出 ...

  3. MyEclipse10配置PyDev进行Python开发

    MyEclipse10配置PyDev进行Python开发 1.下载PyDev 2.7.1  链接如下:    http://jaist.dl.sourceforge.net/project/pydev ...

  4. android基础组件---->Button的使用

    按钮由文本或图标(或文本和一个图标)组成,当用户触摸到它时,会发生一些动作.今天我们开始Button的学习.少年的爱情永远不够用,一杯酒足以了却一件心事. Button的简要说明 根据你是否想要一个带 ...

  5. ipmi监控主机

    author: headsen   chen date: 2018-09-25  20:04:01 IPMI介绍       IPMI(Intelligent Platform Management ...

  6. Flash 用FLASH遮罩效果做图片切换效果

    本教程是关于FLASH应用遮罩效果制作好看的图片切换效果.该教程选用FLASH遮罩中最简单的一种作为例子,当然你可以用自己的想象力来做出更多更好的图片动画.希望本教程能带你带来帮助. 让我们先看看效果 ...

  7. 为listview的item中的元素设置onclick事件

    表达能力比较差,所以现在解释一下标题的意思:listview的列表项,点击的时候触发的是itemOnClick事件,点击后转向到A页:那么,假如在子项中有一个连接是想转到B页,我们该怎么办呢.这样能明 ...

  8. oracle的with as用法

    转自:https://www.cnblogs.com/linjiqin/archive/2013/06/24/3152667.html with as语法–针对一个别名with tmp as (sel ...

  9. 170725、Kafka原理与技术

    本文转载自:http://www.linkedkeeper.com/detail/blog.action?bid=1016 Kafka的基本介绍 Kafka最初由Linkedin公司开发,是一个分布式 ...

  10. Guava增强for循环

    Guava的前身是Google Collections,是Google开发出的一个开源Java常用类库,包含了一些集合的便捷操作API.本文通过一些常用的例子来剖析Guava的奇妙之处. Guava是 ...