课程表

Ember.js 基础

Ember.js 对象模型

Ember.js handlebars模板

Ember.js 路由

Ember.js 组件

Ember.js 控制器

Ember.js 模型

Ember.js 测试

工具箱
速查手册

action触发变化

当前位置:免费教程 » JS/JS库/框架 » Ember.js

组件就像一个相对独立的盒子。在前面的文章中介绍过组件是怎么通过属性传递参数,并且这个属性值你可以在模板或者js代码中获取。

但是到目前为止还没介绍过子组件从父组件中获取数组,在Ember应用中组件之间的通信是通过actions实现的。

跟着下面的步骤来,创建一个组件之间通信的示例。

1,创建组件

创建组件的方法不用我多说,直接使用Ember CLI命令创建即可。

  1. ember g component button-with-confirmation
  2. ember g component user-profile
  3. ember g route button-with-confirmation-route

为了测试方便多增加了一个路由。

下面是组件user-profile的定义,调用组件button-with-confirmation,那么此时user-profile作为父组件,button-with-confirmation作为子组件:

  1. <!-- app/temlates/components/user-profile.hbs -->
  2. {{button-with-confirmation text="Click OK to delete your account"}}

2,在组件类中添加action

要想action能执行需要作如下两步:

  • 在父组件中定义好需要处理的动作(action)。在这个例子中父组件的动作是要删除用户账号并发送一个提示信息到另一个组件。
  • 在子组件中,判断发生什么事件并发出通知信息。在这个例子中当用户点击“确认”按钮之后触发一个外部的动作(删除账户或者发送提示信息)。

下面是实现代码:

实现父组件动作(action)

在父组件中,定义好当用户点击“确认”之后触发的动作。在这个例子中的动作(action)是先找出用户账号再删除。

Ember应用中,每个组件都有一个名为actions的属性。这个属性值是函数,是可以被用户或者子组件执行的函数。

  1. // app/components/user-profile.js
  2. import Ember from 'ember';
  3. export default Ember.Component.extend({
  4. actions: {
  5. userDidDeleteAccount: function() {
  6. console.log(“userDidDeleteAccount…”);
  7. }
  8. }
  9. });

现在已经实现了父组件逻辑,但是并没有告诉Ember这个动作什么时候触发,下一步将实现这个功能。

实现子组件动作(action)

这一步我们将实现当用户点击“确定”之后触发事件的逻辑。

  1. // app/components/button-with-confirmation.js
  2. import Ember from 'ember';
  3. export default Ember.Component.extend({
  4. tagName: 'button',
  5. click: function() {
  6. if (confirm(this.get('text'))) {
  7. // 在这里获取父组件的事件(数据)并触发
  8. }
  9. }
  10. });

3,传递action到组件中

现在我们在user-profile组件中使用onConfirm()方法触发组件button-with-confirmation类中的userDidDeleteAccount事件。

  1. <!-- app/temlates/components/user-profile.hbs -->
  2. {{#button-with-confirmation text="Click OK to delete your account" onConfirm=(action 'userDidDeleteAccount')}}
  3. 执行userDidDeleteAccount方法
  4. {{/button-with-confirmation}}

这段代码的意思是告诉父组件,userDidDeleteAccount方法会通过onConfirm方法执行。

现在你可以在子组件中使用onConfirm方法执行父组件的动作。

  1. // app/components/button-with-confirmation.js
  2. import Ember from 'ember';
  3. export default Ember.Component.extend({
  4. tagName: 'button',
  5. click: function() {
  6. if (confirm(this.get('text'))) {
  7. // 在父组件中触发动作
  8. this.get('onConfirm')();
  9. }
  10. }
  11. });

this.gete(“onConfirm”)从父组件返回一个值onConfirm,然后与()组合成了一个个方法onConfirm()

在模板button-with-confirmation-route.hbs中调用组件。

  1. <!-- app/temlates/button-with-confirmation-route.hbs -->
  2. {{user-profile}}

结果

点击这个button,会触发事件。弹出对话框。再点击“确认”后执行方法userDidDeleteAccount,可以看到浏览器控制台输出了userDidDeleteAccount…,未点击“确认”前或者点击“取消”不会输出这个信息,说明不执行这个方法userDidDeleteAccount

结果截图

结果截图

像普通属性,actions可以组件的一个属性,唯一的区别是,属性设置为一个函数,它知道如何触发的行为。

在组件的actions属性中定义的方法,允许你决定怎么去处理一个事件,有助于模块化,提高组件重用率。

到此,组件这一章节的内容全部介绍完毕了,不知道你看懂了多少?如果有疑问请给我留言一起交流学习,获取是直接去官网学习官方教程。

转载本站内容时,请务必注明来自W3xue,违者必究。
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号