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

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

问题描述

你如何通过事件侦听器传递参数/变量?我已经克服了这个问题相当有效地使用匿名函数;这是一个非常简单的解决方案,但在一天结束的时候,它的气味像一个巨大的漏洞,对功能,我觉得自己的的原生地反正提供的。

通常生活会继续下去,但命运的安排吧,现在我居然需要删除该侦听器,这样当你使用一个匿名函数是一个有点古怪。所以,再一次,我试图找出如何通过传递参数给事件侦听器,以便将事件侦听器可以通过简单地引用功能被删除。

为奇数,因为它看起来,我已经克服了这个问题的以及的,但是,我不喜欢它,累了使用它的。在我看来,这是code气味。但是,它的工作原理就像一个魅力。我存储在调度影片剪辑的变量,对象或什么的。所以,如果我在循环,虽然数据数组,在动态地生成缩略图,我简单地存储在实际的缩略图影片剪辑的数据变量(通常是一个对象具有多个属性)。然后,我可以通过引用访问事件侦听器方法中的所有数据:

 event.target.data 。
在这个例子中,数据是变量保持保持我想要的信息的名称。因为另一个问题,作物,当我不使用这个是,是,当我遍历数组和生成的缩略图点击查看大图像,该指数并不一致。在循环结束时,用我的最后一个索引中的所有thumbails将所有打开的图像。所以,如果我有一个数组12的长度,他们会不顾一切的东西缩略图你点击加载12日的图像。将数据存储到影片剪辑本身,创建一个可靠的参考永远不会改变。

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


下面是一些饮食的例子。如果需要的话我可以发布更多详细的例子。所有的例子都描绘了一个缩略图点击时加载一个大的图像。

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

tempThumb.addEventListener(MouseEvent.CLICK,的LoadImage);

公共职能的LoadImage(_event:MouseEvent)方法:无效
{
    //我没有变_url和preparing一个热水澡感冒刀片
}

使用匿名函数:

tempThumb.addEventListener(MouseEvent.CLICK,功能(_event:的MouseEvent){的LoadImage(large.jpg);});

公共职能的LoadImage(_url:字符串):无效
{
    //我得到的变量_url和包装走的刀片
}

不使用匿名函数,但是使用将数据存储到影片剪辑调度事件的我的臭勒prechaun技术

tempThumb.data =large.jpg;

tempThumb.addEventListener(MouseEvent.CLICK,的LoadImage);

公共职能的LoadImage(_event:MouseEvent)方法:无效
{
    跟踪(event.target.data);
    //我可以访问变量
}

我不避让编程术语,所以我勒prechaun技术上面冠以。存储/供以后使用/访问对象的隐藏变量。它解决了我所有的问题和工作得非常好。但是,如果有更好的方式来做到这一点,我想知道的的方式。

解决方案

(我认为你需要阅读这个答案

对于你的疑问,你觉得你自己:你的文件prechaun的臭(双关语)。想象一下,这是一个精灵,而不是一个影片剪辑。它不是动态的,所以除非你做可以不加属性,如理查德说

下面是更好的解决方案,你想,根据你的例子:

  tempThumb.addEventListener(MouseEvent.CLICK,的LoadImage(large.jpg));

公共职能的LoadImage(_url:字符串):函数{
  复位功能(_event:MouseEvent)方法:无效{
    //现在你有两个变量_url和_event在这里!
  }
}
 

但你希望能够删除监听器,所以:

  VAR functionLoadImage:功能=的LoadImage(large.jpg);
tempThumb.addEventListener(MouseEvent.CLICK,functionLoadImage);

公共职能的LoadImage(_url:字符串):函数{
  复位功能(_event:MouseEvent)方法:无效{
    //现在你有两个变量_url和_event在这里!
  }
}

//trace(tempThumb.hasEventListener(MouseEvent.CLICK));
tempThumb.removeEventListener(MouseEvent.CLICK,functionLoadImage);
//trace(tempThumb.hasEventListener(MouseEvent.CLICK));
 

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.

解决方案

(I think you'll want to read this answer.)

Regarding your doubt, you felt it yourself: your leprechaun's smelly (pun). Imagine it's a Sprite instead of a MovieClip. It isn't dynamic, so you can't add properties unless you do like Richard said.

Here's the better solution you wished, based on your example:

tempThumb.addEventListener(MouseEvent.CLICK, loadImage("large.jpg"));

public function loadImage(_url:String):Function {
  return function(_event:MouseEvent):void {
    // Now you have both variables _url and _event here!
  }
}

But you want to be able to remove listeners, so:

var functionLoadImage:Function = loadImage("large.jpg");
tempThumb.addEventListener(MouseEvent.CLICK, functionLoadImage);

public function loadImage(_url:String):Function {
  return function(_event:MouseEvent):void {
    // Now you have both variables _url and _event here!
  }
}

//trace(tempThumb.hasEventListener(MouseEvent.CLICK));
tempThumb.removeEventListener(MouseEvent.CLICK, functionLoadImage);
//trace(tempThumb.hasEventListener(MouseEvent.CLICK));

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

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