一、概述

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. Effective C++ Item 14 Think carefully about copying behavior in resource-managing classe

    In C++, the only code that guaranteed to be executed after an exception is thrown are the destructor ...

  2. Nginx(二)-- 配置文件之虚拟主机配置

    1.配置文件与解释 #user nobody; worker_processes 1; # 设置工作子进程,默认是1个工作子进程,可以修改,一般设置为CPU的总核数 #error_log logs/e ...

  3. Python 爬虫实战

    图片爬虫实战 链接爬虫实战 糗事百科爬虫实战 微信爬虫实战 多线程爬虫实战

  4. #pragam预处理分析

    #pragma是编译器指示字,用域指示编译器完成一些特定动作, #pragma所定义的很多指示字是编译器和操作系统特有的 #pragma在不同的编译器间是不可移植的 预处理器将忽略它不认识的#prag ...

  5. jquery ajax返回html乱码解决

    情景: index.html 使用如下代码访问另外一个html片段 1 2 3 4 5 6 7 8 9 10 11 12 13 function rewritedata(id){     var uu ...

  6. PyQt4信号与槽

    事件 事件(Events)是GUI程序中很重要的一部分.它由用户或系统产生.当我们调用程序的exec_()方法时,程序就会进入主循环中.主循环捕获事件并将它们发送给相应的对象进行处理.奇趣公司(Tro ...

  7. 集群应用Session一致性实现的三种方案

    转自:http://blog.csdn.net/zwx521515/article/details/78679679 https://www.cnblogs.com/study-everyday/p/ ...

  8. List转换为数组Array的方法

    List<String> list = new ArrayList<String>(); list.add("str1"); list.add(" ...

  9. HTML5文件拖拽上传记录

    JS文件: var FileName = ""; var FileStr = ""; (function () { function $id(id) { ret ...

  10. 【顽固BUG】Visual Studio 2015 + TestDriven.NET-3.8.2860_Personal_Beta 调用的目标发生了异常。

    前言 突然怎么弄也无法断点调试了 输出如下: ------ Test started: Assembly: Server5.V2.dll ------ 调用的目标发生了异常. 而且网站运行提示: -- ...