在 Flex/AS3 中将属性附加到事件

发布于 2024-08-10 17:38:07 字数 422 浏览 10 评论 0原文

我有一个需要与事件一起传递的参数。在尝试通过扩展类将其放置在类型上失败后,我在另一个 SO 问题中被建议编写一个自定义事件。

我之前尝试过,但不知道如何发送它,因为我只想在 FileReference 上使用正常的 Event.COMPLETE 事件发送另一个参数。我不能让 Event.COMPLETE 触发,然后分派我的事件,因为那样我就会在同一条船上......所需的参数不会在初始 Event.COMPLETE 中 对象,因此我将无法调度一个新事件,将该参数传递给实际执行工作的代码。

非常感谢所有对此的帮助,非常需要它。谢谢。

基本上,我需要完全按原样替换 Event.COMPLETE 功能,我只需要一个能够保存我的额外属性的事件。请告诉我有什么办法可以做到这一点。

I have a parameter that needs to be passed along with an event. After unsuccessful attempts to place it on the type by extending the class, I've been advised in another SO question to write a custom event.

I tried this before but I couldn't figure out how to send it, because I just want to send another parameter with a normal Event.COMPLETE event on a FileReference. I can't have Event.COMPLETE fire and then dispatch my event because then I'll be in the same boat ... the needed parameter won't be in the initial Event.COMPLETE object so I won't be able to dispatch a new event passing that parameter on to the code that actually performs the work.

All help on this is greatly appreciated, need it badly. Thanks.

Basically, I need to replace the Event.COMPLETE functionality exactly as it is, I just need an event that will hold my extra property. Please tell me there's some way to do this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

望笑 2024-08-17 17:38:07

首先,您需要照常向 Event.COMPLETE 添加一个事件侦听器,因为它将自动分派,您对此无能为力。在该事件处理程序中,您可以调度您自己的自定义,即使应用程序的其余部分使用附加的自定义属性进行侦听。这是一个简单的示例:

您的事件处理代码如下所示:

yourObject.addEventListener(Event.COMPLETE, function(event:Event):void{

  var myEvt:MyEvent = new MyEvent(MyEvent.COMPLETE, myCustomProperty);
  dispatchEvent( myEvt );

})

MyEvent 类如下所示:

package
{

  import flash.events.Event;

  public class MyEvent extends Event
  {

    public static const COMPLETE:String = "MyEventComplete";

    var myCustomProperty:*;

    public function MyCustomEvent(type:String, prop:*) :void
    {

      myCustomProperty = prop;

      super(type);
      // if you want your event to bubble 
      // be sure to specify : super(type, true)
    }

   //override clone() so your event bubbles correctly
   public override function clone() :MyEvent {

     return new MyEvent(this.type, this.myCustomProperty)

   }
  }
}

您现在可以像任何其他事件一样为自定义事件添加事件侦听器:

addEventListener(MyCustomEvent.COMPLETE, someFunction);

first you need to add an event listener to the Event.COMPLETE as normal since it is going to be dispatched automatically and theres nothing you can do about it. Inside that event handler you can then dispatch your own custom even that the rest of the application listens for with your custom property attached to it. heres a quick example:

your event handling code would look like:

yourObject.addEventListener(Event.COMPLETE, function(event:Event):void{

  var myEvt:MyEvent = new MyEvent(MyEvent.COMPLETE, myCustomProperty);
  dispatchEvent( myEvt );

})

and the MyEvent class looks like:

package
{

  import flash.events.Event;

  public class MyEvent extends Event
  {

    public static const COMPLETE:String = "MyEventComplete";

    var myCustomProperty:*;

    public function MyCustomEvent(type:String, prop:*) :void
    {

      myCustomProperty = prop;

      super(type);
      // if you want your event to bubble 
      // be sure to specify : super(type, true)
    }

   //override clone() so your event bubbles correctly
   public override function clone() :MyEvent {

     return new MyEvent(this.type, this.myCustomProperty)

   }
  }
}

you can now add event listeners for your custom event just like any other event:

addEventListener(MyCustomEvent.COMPLETE, someFunction);
流绪微梦 2024-08-17 17:38:07

我不是专家,但以下内容对我有用。 (为保护无辜者,化名)。

这是我的自定义事件之一:

import flash.events.Event;
public class SomethingHappenedEvent extends Event {
    public static const SOMETHING_COMPLETE:String = "PictureTakenComplete";
    public var passedValue:int;

    public function SomethingHappenedEvent(i:int) {
        passedValue = i;
        super(SOMETHING_COMPLETE);      
    }
}

我设置了一个侦听器来侦听我的自定义事件。当自定义事件触发时,我的 doSomething() 函数将被执行。以下是我的设置方法:

mySecret.addEventListener(SomethingHappenedEvent.SOMETHING_COMPLETE,doSomething);

doSomething() 函数内,我可以访问事件的 passedValue 参数:

private function doSomething(e:SomethingHappenedEvent):void {
    trace("did something: " + e.passedValue);
}

最后,以下是我在代码中其他位置分派自定义事件的方法,传入一个参数:

this.dispatchEvent(new SomethingHappenedEvent(myCounter));

I'm not an expert, but the following is working for me. (Names changed to protect the innocent).

Here is one of my custom events:

import flash.events.Event;
public class SomethingHappenedEvent extends Event {
    public static const SOMETHING_COMPLETE:String = "PictureTakenComplete";
    public var passedValue:int;

    public function SomethingHappenedEvent(i:int) {
        passedValue = i;
        super(SOMETHING_COMPLETE);      
    }
}

I have a listener set up to listen for my custom event. When the custom event fires, my doSomething() function is executed. Here's how I set that up:

mySecret.addEventListener(SomethingHappenedEvent.SOMETHING_COMPLETE,doSomething);

Inside the doSomething() function I have access to event's passedValue parameter:

private function doSomething(e:SomethingHappenedEvent):void {
    trace("did something: " + e.passedValue);
}

Finally, here is how I dispatch a custom event elsewhere in the code, passing in a parameter:

this.dispatchEvent(new SomethingHappenedEvent(myCounter));
顾挽 2024-08-17 17:38:07
  1. 使用数据参数创建自定义事件
  2. 您的类((例如 MyObject)应该使用 addEventListener 捕获 Event.COMPLETE 并使用其中的数据分派您的自定义事件 MyCustomEvent 在
  3. 其他地方捕获您的 MyCustomEvent(例如在 MyAnotherObject 中)

MyCustomEvent.as

package
{
 import flash.events.Event;

 public class MyCustomEvent extends Event
 {
  public static const MY_CUSTOM_EVENT_COMPLETE:String = "myCustomEventComplete";

  public var data:Object;

  // Constructor
  public function CaretEvent(type:String, data:Object, bubbles:Boolean=false, cancelable:Boolean=false)
  {
   super(type, true, cancelable);

   this.data = data;
  }

  // Public Methods
  public override function clone():MyCustomEvent
  {
   return new MyCustomEvent(type, data, bubbles, cancelable);
  }
 }
}

MyObject.as

package
{
 import flash.events.EventDispatcher;
 import flash.events.Event;

 public class MyObject extends EventDispatcher
 {
  // Contructor
  public function MyObject()
  {
   addEventListener(Event.COMPLETE, onComplete)
  }

  // Private Methods
  private function onComplete(event:Event):void
  {
   dispatchEvent(new MyCustomEvent(MyCustomEvent.MY_CUSTOM_EVENT_COMPLETE, "This is my custom data"));
  }
 }
}

MyAnotherObject 。作为

package
{
 import flash.events.Event;
 import flash.events.EventDispatcher;

 public class MyAnotherObject extends EventDispatcher
 {
  // Constructor
  public function MyAnotherObject()
  {
   addEventListener(MyCustomEvent.MY_CUSTOM_EVENT_COMPLETE, onMyCustomEventComplete)
  }

  // Private Methods
  private function onMyCustomEventComplete(event:MyCustomEvent):void
  {
   trace(event.data);
  }
 }
}
  1. Create Custom Event with data parameter
  2. Your class ( (for example MyObject) should catch Event.COMPLETE using addEventListener and dispatch your custom event MyCustomEvent with your data in it
  3. Catch your MyCustomEvent somewhere else (for example in MyAnotherObject)

MyCustomEvent.as

package
{
 import flash.events.Event;

 public class MyCustomEvent extends Event
 {
  public static const MY_CUSTOM_EVENT_COMPLETE:String = "myCustomEventComplete";

  public var data:Object;

  // Constructor
  public function CaretEvent(type:String, data:Object, bubbles:Boolean=false, cancelable:Boolean=false)
  {
   super(type, true, cancelable);

   this.data = data;
  }

  // Public Methods
  public override function clone():MyCustomEvent
  {
   return new MyCustomEvent(type, data, bubbles, cancelable);
  }
 }
}

MyObject.as

package
{
 import flash.events.EventDispatcher;
 import flash.events.Event;

 public class MyObject extends EventDispatcher
 {
  // Contructor
  public function MyObject()
  {
   addEventListener(Event.COMPLETE, onComplete)
  }

  // Private Methods
  private function onComplete(event:Event):void
  {
   dispatchEvent(new MyCustomEvent(MyCustomEvent.MY_CUSTOM_EVENT_COMPLETE, "This is my custom data"));
  }
 }
}

MyAnotherObject.as

package
{
 import flash.events.Event;
 import flash.events.EventDispatcher;

 public class MyAnotherObject extends EventDispatcher
 {
  // Constructor
  public function MyAnotherObject()
  {
   addEventListener(MyCustomEvent.MY_CUSTOM_EVENT_COMPLETE, onMyCustomEventComplete)
  }

  // Private Methods
  private function onMyCustomEventComplete(event:MyCustomEvent):void
  {
   trace(event.data);
  }
 }
}
最佳男配角 2024-08-17 17:38:07

您能否发布一些代码,以便我们了解您是如何解决这个问题的?

不过,我强烈建议您阅读 flex live 文档 关于创建和使用自定义事件。

这里是一个简单的自定义基于事件的应用程序,看看源代码,它可能就是您正在寻找的。

Could you post some code so we can see how you are approaching this?

However I would highly recommend reading the flex live docs on creating and using custom events.

Here is a simple custom event based application, Take a look at the source, it might be what you are looking for.

剩余の解释 2024-08-17 17:38:07

我通常会做这样的事情,也许不是最好的方法,但你应该知道这种方法是可用的......

myObj.addEventListener("creationComplete", function(e:Event):void{otherFunction(e, "another Arg")});

I usually do something like this, maybe not the best approach, but you should know this way is available...

myObj.addEventListener("creationComplete", function(e:Event):void{otherFunction(e, "another Arg")});
时间你老了 2024-08-17 17:38:07

从事情的声音来看,如果您需要的只是另一个属性,您可能不需要扩展 FileReference、Event.COMPLETE 等......以下内容是否适合您的目的?如果没有,请向我展示一些代码,以便我们可以在同一页面上。

public function go():void {
    var downloadURL:URLRequest = new URLRequest("http://example.com/big.jpg");
    var otherParam:String = "another param";
    var fileRef:FileReference = new FileReference();
    fileRef.addEventListener(Event.COMPLETE, function(e:Event):void {
                                                handleLoad(e,otherParam); });
    fileRef.download(downloadURL);
}

public function handleLoad(e:Event,whatelse:String):void {
    Alert.show("we just loaded " + e.target + " with param " + whatelse);
}

这将在完成这项工作的 handleLoad() 函数中获取一个额外的参数 (whatelse)。或者你是说在下载时你没有附加参数?

From the sound of things, you may not need to extend FileReference, Event.COMPLETE, etc. if all you need is another property... will the following work for your purposes? If not, show me some code so we can be on the same page.

public function go():void {
    var downloadURL:URLRequest = new URLRequest("http://example.com/big.jpg");
    var otherParam:String = "another param";
    var fileRef:FileReference = new FileReference();
    fileRef.addEventListener(Event.COMPLETE, function(e:Event):void {
                                                handleLoad(e,otherParam); });
    fileRef.download(downloadURL);
}

public function handleLoad(e:Event,whatelse:String):void {
    Alert.show("we just loaded " + e.target + " with param " + whatelse);
}

That will get an extra parameter (whatelse) into the handleLoad() function that does the work. Or are you saying that at the time of the download you don't have the additional parameter?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文