如何处理ember.js组件中的自定义事件? [英] how to deal with custom events in ember.js component?

查看:175
本文介绍了如何处理ember.js组件中的自定义事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚接触到Ember.js,而且我有一些问题要理解它的理念。我知道行动,数据下降,但在现实生活中,让我说我已经 Fotorama 初始化在我的画廊组件(我不知道这是否可以,但我在 didInsertElement 方法中)。这个图书馆有自己的事件。他们可以在简单的JS中看起来像这样:

  $('fotorama')。on('fotorama:ready' (e,fotorama){}); 

或:

 ('fotorama:show',function(){}); code> $ 

但我觉得在Ember中,应该以某种方式映射到组件中的操作。
我的问题是:怎么样?我需要在这些动作中触发一些动作(被其他组件或路由器所捕获)。所以我认为应该是这样的: this.sendAction('actionName',actionParams);

解决方案

您可以保留组件引用来调用sendAction方法。

  didInsertElement(){
this._super(...参数);
var _this = this;
this。$('。fotorama')。on('fotorama:show',function(){
_this.sendAction('actionName',actionParams);
});
}
willDestroyElement(){
this._super(... arguments);
这个$('。fotorama')。off('fotorama:show')
}

如果我们找到一个更好的答案这个问题。我会高兴地删除我的答案。


I'm new to Ember.js and I've got some problems to understand its philosophy. I know actions up, data down but in real life, lets say I have Fotorama initialized in my-gallery component (I don't know if that is ok, but I did it in didInsertElement method). This library has its own events. They could look like this in plain JS:

$('.fotorama').on('fotorama:ready', function (e, fotorama) {});

or:

$('.fotorama').on('fotorama:show', function () {});

but I feel in Ember, those should be somehow mapped into actions in component. My question is: how? I need to fire some actions (to be catched by another components or maybe a router) inside those actions. So I think it should be like this: this.sendAction('actionName', actionParams);

解决方案

You can keep component reference to call sendAction method.

didInsertElement(){
 this._super(...arguments);
 var _this=this;
 this.$('.fotorama').on('fotorama:show', function () {
  _this.sendAction('actionName', actionParams);
 });
}
willDestroyElement(){
 this._super(...arguments);
 this.$('.fotorama').off('fotorama:show')
}

If we find an better answer to this question. I will happily remove my answer.

这篇关于如何处理ember.js组件中的自定义事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆