将参数传递给事件侦听器/处理程序 [英] Passing parameters to event listeners / handlers

查看:33
本文介绍了将参数传递给事件侦听器/处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何通过事件监听器传递参数/变量?我已经使用匿名函数相当有效地克服了这个问题;这是一个非常简单的解决方案,但归根结底,它闻起来像是一个巨大的功能漏洞,我觉得无论如何都应该在本地提供.

通常生活会继续,但命中注定,现在我实际上需要移除监听器,当你使用匿名函数时这样做有点时髦.所以,再次,我试图弄清楚如何将参数传递给事件侦听器,以便通过简单地引用函数来删除事件侦听器.

尽管看起来很奇怪,但我也克服了这个问题,但是,我不喜欢它并且厌倦了使用它.在我看来,这是代码异味.但它就像一个魅力.我将变量、对象或任何内容存储在调度的 MovieClip 上.因此,如果我循环遍历数据数组,即时生成缩略图,我只需将数据变量(通常是具有多个属性的对象)存储在实际缩略图 MovieClip 中.然后我可以通过引用访问事件侦听器方法中的所有数据:

event.target.data

.在这个例子中,data"是保存我想要的信息的变量的名称.因为当我不使用它时出现的另一个问题是,当我循环遍历数组并生成单击以查看大图像的缩略图时,索引不一致.在循环结束时,所有缩略图都将使用i"的最后一个索引打开一个图像.因此,如果我有一个长度为 12 的数组,无论您单击什么缩略图,它们都会加载第 12 个图像.将数据存储到 MovieClip 本身中,创建了一个永不改变的可靠参考.

这已经困扰了我一段时间了.基本上我想知道的是,这是好的做法吗,有没有更好的解决方案?

以下是一些饮食示例.如有必要,我可以发布更详细的示例.所有示例都描述了一个缩略图,点击时会加载一个大图像.




不使用匿名函数(问题):

<前>tempThumb.addEventListener(MouseEvent.CLICK, loadImage);公共函数 loadImage(_event:MouseEvent):void{//我没有变量_url,准备用冷刀片洗个热水澡}

匿名函数的使用:

<前>tempThumb.addEventListener(MouseEvent.CLICK, function(_event:MouseEvent) { loadImage("large.jpg"); } );公共函数 loadImage(_url:String):void{//我得到了变量 _url 并打包了剃须刀片}

不使用匿名函数,而是使用我的臭妖精技术将数据存储到调度事件的 MovieClip 中

<前>tempThumb.data = "大.jpg";tempThumb.addEventListener(MouseEvent.CLICK, loadImage);公共函数 loadImage(_event:MouseEvent):void{跟踪(事件.目标.数据);//我可以访问变量}

我对编程术语一窍不通,所以我将上面的内容称为 Leprechaun Technique.在对象中存储/隐藏变量以供以后使用/访问.它解决了我所有的问题,而且效果非常好.但是,如果有更好的方法,我想知道那种方式.

解决方案

是的,我明白你的意思.我所做的是扩展事件并调度自定义事件.这样我就可以在包含必要数据的自定义事件中创建属性.它看起来很干净,但确实需要一些努力.这并不理想,但我不知道有什么更好的方法.

How do you pass parameters / variables through event listeners? I've overcome this problem fairly effectively using anonymous functions; which is an incredibly simple solution, but at the end of the day, it smells like a giant loophole for functionality that I feel should be provided natively anyways.

Normally life would go on, but as fate would have it, now I actually need to remove the listener, and doing so when you've using an anonymous function is a bit funky. So, once again, I'm trying to figure out how to pass parameters through to event listeners so that the event listeners can be removed by simply referencing the function.

As odd as it may seem, I've overcome this issue as well, BUT, I don't like it and tired of using it. In my opinion it is code smell. But it works like a charm. I store the variable, object or whatever on the dispatching MovieClip. So, if I'm looping though an array of data, generating thumbnails on the fly, I simply store the data variable (normally an object with multiple properties) in the actual thumbnail MovieClip. Then I can access all the data in an event listener method by referencing:

event.target.data

. In this example, "data" is the name of the variable holding holding the information I want. Because another issue that crops up when I don't use this is, is that when I'm looping through an array and generating thumbnails that click to view large images, the index is not consistent. At the end of the loop, ALL the thumbails will all open an image using the last index of "i". So if I had an array with a length of 12, they'd all load the 12th image regardless of what thumbnail you clicked on. Storing the data into the MovieClip itself, creates a solid reference that never changes.

This has been bothering me for some time now. Basically what I want to know is, is this good practice, are there better solutions out there?

Below are some diet examples. I can post more detailed examples if necessary. All examples depict a thumbnail that loads a large image when clicked.




Without the use of an anonymous function (the problem):

tempThumb.addEventListener(MouseEvent.CLICK, loadImage);

public function loadImage(_event:MouseEvent):void  
{
    // I don't have the variable _url and preparing a hot bath with a cold blade  
}

Use of an anonymous function:

tempThumb.addEventListener(MouseEvent.CLICK, function(_event:MouseEvent) { loadImage("large.jpg"); } );

public function loadImage(_url:String):void  
{
    // I got the variable _url and packing away the razor blades  
}

Without the use of an anonymous function, but using my smelly Leprechaun technique of storing the data into the MovieClip dispatching the event

tempThumb.data = "large.jpg";

tempThumb.addEventListener(MouseEvent.CLICK, loadImage);

public function loadImage(_event:MouseEvent):void  
{
    trace(event.target.data);
    // I can access the variable  
}

I'm not clued up on programming terminology, so I've dubbed the above the Leprechaun Technique. Storing / hiding variables in objects for later use / access. It solves all my problems and works amazingly well. But, if there is a better way to do it, I want to know that way.

解决方案

Yes I know what you mean. What I do is to extend the event and so dispatch a custom event. This way I can create properties in the custom event that contain the necessary data. It seems clean but does require a bit of effort. It is not ideal but I don't know of a significantly better way.

这篇关于将参数传递给事件侦听器/处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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