作者:乃君敏睿64 | 来源:互联网 | 2023-02-07 16:55
ItsnotamatteroflifeordeathbutIwonderifthiscouldbepossible:这不是生死攸关的问题,但我想知道这是否可行:I
It's not a matter of life or death but I wonder if this could be possible:
这不是生死攸关的问题,但我想知道这是否可行:
I got a couple of events from one type of custom event (FormEvent) now I got a FormListener that listens to all those events and handles them according to the event type. Instead of adding one eventListener at the time I wish to add all events at once.
我从一种类型的自定义事件(FormEvent)中获得了一些事件,现在我得到了一个FormListener,它监听所有这些事件并根据事件类型处理它们。我希望一次添加所有事件,而不是添加一个eventListener。
so now it looks like this:
所以现在它看起来像这样:
private function addListeners():void {
addEventListener(FormEvent.SHOW_FORM, formListener);
addEventListener(FormEvent.SEND_FORM, formListener);
addEventListener(FormEvent.CANCEL_FORM, formListener);
}
private function formListener(event:formEvent):void {
switch(event.type){
case "show.form":
// handle show form stuff
break;
case "send.form":
// handle send form stuff
break;
case "cancel.form":
// handle cancel form stuff
break;
}
}
but instead of adding every event one at the time I would rather be doing something like
但是,我宁愿做一些类似的事情,而不是每次添加一个事件
private function addListeners():void {
addEventListener(FormEvent.*, formListener);
}
I wonder if something like this is possible, i would love it. I work with loads of events :)
我想知道这样的事情是否可行,我会喜欢它。我处理大量事件:)
2 个解决方案
7
You only really need one event listener in this case anyhow. That listener will be listening for any change with the form and a parameter equal to what the change was becomes available to the event listener function. I will show you, but please remember that this is a pseudo situation and normally I wouldn't dispatch an event off of something as simple as a method call because the dispatch is implied so there is no real need to listen for it.
无论如何,在这种情况下你只需要一个事件监听器。该监听器将使用表单和参数监听任何更改,该参数等于事件监听器函数可用的更改。我会告诉你,但请记住,这是一个伪情况,通常我不会发送一个像方法调用这样简单的事件,因为调度是暗示的,所以没有真正需要听它。
First the Custom Event
首先是自定义事件
package com.yourDomain.events
{
import flash.events.Event;
public class FormEvent extends Event
{
//Public Properties
public static const CANCEL_FORM:int = "0";
public static const SHOW_FORM:int = "1";
public static const SEND_FORM:int = "2";
public static const STATE_CHANGED:String = "stateChanged";
//Private Properties
private var formState:int;
public function FormEvent(formState:int):void
{
super(STATE_CHANGED);
formState = formState;
}
}
}
So we have just created our custom event class and we have set it up so that we can catch the state through the listener function as I will demonstrate once done with the pseudo form class that will dispatch the for said custom event.
所以我们刚刚创建了我们的自定义事件类,我们已经设置了它,以便我们可以通过监听器函数捕获状态,因为我将演示一旦使用将为所述自定义事件调度的伪表单类。
Remember that this is all hypothetical as I have no idea what your code looks like or how your implementing things. What is important is to notice that when I dispatch the event I need to send a parameter with it that reflects what the new state is.
请记住,这都是假设,因为我不知道您的代码是什么样的或者您的实现方式。重要的是要注意,当我发送事件时,我需要发送一个反映新状态的参数。
package com.yourDomain.ui
{
import flash.events.Event;
import flash.events.EventDispatcher;
import com.yourDomain.events.FormEvent;
public class Form extends EventDispatcher
{
public function Form():void
{
//Anything you want form to do upon instantiation goes here.
}
public function cancelForm():void
{
dispatchEvent(new Event(FormEvent.CANCEL_FORM);
}
public function showForm():void
{
dispatchEvent(new Event(FormEvent.SHOW_FORM);
}
public function sendForm():void
{
dispatchEvent(new Event(FormEvent.SEND_FORM);
}
}
}
And finally we create the document class that will listen for it. Please know that I realize it isn't logical to create a listener that fires when you call a method of a class because you obviously know you called the method, but for this example it will due.
最后我们创建了将要监听它的文档类。请注意,我知道创建一个在调用类的方法时触发的侦听器是不合逻辑的,因为你显然知道你调用了该方法,但是对于这个例子它将会到期。
package com.yourDomain.ui
{
import com.yourDomain.ui.Form;
import com.yourDomain.events.FormEvent;
//Form is in the same package so we need not import it.
public class MainDocumentClass
{
private var _theForm:Form;
public function MainDocumentClass():void
{
_theForm = new Form();
_theForm.addEventListener(FormEvent.STATE_CHANGED, onFormStateChange, false, 0, true);
/*
The following three method calls each cause the
FormEvent.STATE_CHANGE event to be dispatched.
onFormStateChange is notified and checks what
the last change actually was.
*/
_theForm.cancelForm();
_theForm.showForm();
_theForm.sendForm();
}
private function onFormStateChange(e:FormEvent):void
{
switch(e.formState)
{
case CANCEL_FORM:
trace('The form was canceled');
break;
case SHOW_FORM:
trace('The form was revealed');
break;
case SEND_FORM:
trace('The form was sent');
break;
}
}
}
}
I hope that this was helpful, its late and I may have to revise some things later, but this should help get an understanding of how to make your own events and to customize how things work.
我希望这很有用,它很晚,我可能不得不稍后修改一些内容,但这应该有助于理解如何制作自己的事件并定制工作方式。