-
Custom eventhandler giving more headaches
Hi all
I'm having huge difficulties getting my custom eventhandler to work. For some reason my eventhandler doesn't get triggered. My eventhandler should get triggered, when my form is submitted. Here is what I have got so far:
Custom event class called submitEvent.as:
Code:
package
{
import flash.events.Event;
public class submitEvent extends Event
{
public static const ON_FORM_SUBMIT:String = "onFormSubmit";
public function submitEvent(type:String)
{
super(type);
}
public override function clone():Event
{
return new submitEvent(type);
}
}
}
When form is submitted, dispatch event from submitForm class file:
Code:
dispatchEvent(new submitEvent(submitEvent.ON_FORM_SUBMIT));
Listener set up in document class file:
Code:
var emailSubmitform:submitForm = new submitForm();
emailSubmitform.addEventListener(submitEvent.ON_FORM_SUBMIT, onFormSubmit);
function onFormSubmit(e:submitEvent):void
{
trace("FORM IS SUBMITTED");
}
addChild(emailSubmitform);
-
Senior Member
change your event class file to this:
Code:
package
{
import flash.events.Event;
public class submitEvent extends Event
{
public static const ON_FORM_SUBMIT:String = "onFormSubmit";
private var command:String;
public function submitEvent(command:String)
{
super(ON_FORM_SUBMIT);
this.command = command;
}
}
}
That should work. At least it does for me anyway.
-
Senior Member
and your dispatch to this:
Code:
dispatchEvent(new submitEvent("onFormSubmit"));
-
hmm .. doesn't seem to have any effect :-(
-
Senior Member
Zip up your source, and attach it, and I'd be more than happy to take a look to see what X factor I can find.
-
That would be a big help, since I've been sending a lot of time now figuring out how this works. A working example would really explain a lot :-)
Thanks a lot
I've uploaded the files at
www.larsliin.dk/Custom_eventhandler.zip
-
Senior Member
Okay, here we go, instead of re-attaching, I'll just post code:
Your ChristmasCountdown.as
Code:
package
{
import flash.display.MovieClip;
import flash.events.*;
import submitEvent;
public class christmasCountdown extends MovieClip
{
private var emailSubmitform:submitForm = new submitForm();
public function christmasCountdown():void
{
emailSubmitform.x = 100;
emailSubmitform.y = 150;
addChild(emailSubmitform);
emailSubmitform.addEventListener(submitEvent.ON_FORM_SUBMIT, onFormSubmitHandler);
}
private function onFormSubmitHandler(e:submitEvent):void
{
emailSubmitform.removeEventListener(submitEvent.ON_FORM_SUBMIT, onFormSubmitHandler);
trace("FORM IS SUBMITTED");
}
}
}
Your submitEvent.as
Code:
package
{
import flash.events.Event;
public class submitEvent extends Event
{
public static const ON_FORM_SUBMIT:String = "onFormSubmit";
private var command:String;
public function submitEvent(command:String)
{
super(ON_FORM_SUBMIT);
this.command = command;
}
}
}
Your submitForm.as
Code:
package {
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.events.KeyboardEvent;
import flash.net.Responder;
import flash.net.NetConnection;
import flash.text.TextField;
import flash.events.*;
import submitEvent;
public class submitForm extends MovieClip {
private var visitorName:String = "";
private var visitorEmail:String = "";
public function submitForm():void {
// add eventlisteners
this.addEventListener(KeyboardEvent.KEY_UP, submitKeyHandler);
submitBtn_btn.addEventListener(MouseEvent.CLICK, submitBtnHandler);
}
// submit keyboard handler
private function submitKeyHandler(e:KeyboardEvent):void {
// keycode 13: enter-key
if (e.keyCode == 13) {
submitHandler();
}
}
// submit button handler
private function submitBtnHandler(e:MouseEvent):void {
submitHandler();
}
// submit user input
private function submitHandler():void {
visitorName = nameTxt_txt.text;
visitorEmail = emailTxt_txt.text;
// submit input if 'validate-input' returns true
if (isEmailValid(visitorEmail) != false && visitorName != "") {
submitUserData();
} else {
trace("INPUT ERROR");
return;
}
}
// check if email is valid
private function isEmailValid(email:String):Boolean {
var emailExpression:RegExp = /^[a-z][\w.-]+@\w[\w.-]+\.[\w.-]*[a-z][a-z]$/i;
return emailExpression.test(email);
}
// submit data
private function submitUserData():void {
dispatchEvent(new submitEvent("Submit"));
trace("dispatching Event Here");
}
}
}
It was tested and is working for me. Let me know if this helps.
-
That's exactly what I need! Thanks a lot.
Could you perhaps just explain, what I did wrong? I noticed, that you have moved the dispatchEvent ..
-
Senior Member
Sure, I'm not that great at explanations though so please feel free to ask if you need further explanation.
Anyway, what I did was this:
In the submitForm.as file:
You had all your function within the constructor, so I split them up. In the dispatchEvent, I just changed the string dispatching. To be honest, it doesn't matter what it is as long as it's nothing conflicting. So in actuality, you could type in dispatchEvent(new submitEvent("WildSpaceMonkeys")); and it would work.
Then of course I added a trace function just for clarification there.
The submit event, all you needed was the constructor as we handled earlier.
Then in your document class your event listeners were tied to the main document class, and the main class never dispatches the Event. The submitForm class dispatches the event, so you have to have that class listen for the event to be dispatched. Then after it was dispatched I removed the Event Listener.
Make sense?
Let me know.
-
That makes perfect sense.
Again thanks a lot. You've have just made it all much more clear to me.
Thanks.
-
Senior Member
No Problem, glad to help.
-
One last question though:
What if I wanted to add a few arguments, when I dispatch this event? Thought it would something like
Code:
package
{
import flash.events.Event;
public class submitEvent extends Event
{
public static const ON_FORM_SUBMIT:String = "onFormSubmit";
private var command:String;
public function submitEvent(command:String)
{
super(ON_FORM_SUBMIT);
this.command = command;
// ADDING MY ARGUMENT
this.testArg = "TEST";
}
}
}
But this only returns
Code:
[Event type="onSubmitEvent" bubbles=false cancelable=false eventPhase=2]
-
Senior Member
Well, this would be my guess, as I just got into this myself and am honestly learning with you, LOL.
Code:
package
{
import flash.events.Event;
public class submitEvent extends Event
{
public static const ON_FORM_SUBMIT:String = "onFormSubmit";
private var command:String;
private var testArg:String;
public function submitEvent(command:String, tArg:String)
{
super(ON_FORM_SUBMIT);
this.command = command;
// ADDING MY ARGUMENT
this.testArg = tArg;
}
}
}
Let me know if that helps
-
Glad to hear that we're actually both benefiting from this.
I tried what you suggested, but still only get
Code:
[Event type="onFormSubmit" bubbles=false cancelable=false eventPhase=2]
Thought that I would get something like
Code:
[Event type="onFormSubmit" testArg="SOME VALUE" bubbles=false cancelable=false eventPhase=2]
And would be able to access it with something like
Code:
private function onFormSubmitHandler(e:submitEvent):void
{
trace(e.testArg);
}
-
The event class has a custom toString method (otherwise you'd just get [Event, event]) - you can override it to see the properties you want:
PHP Code:
public override function toString():String{
return "[submitEvent type=" + type + " testArg=" + testArg + " bubbles="+ bubbles + " cancelable=" + cancelable + " eventPhase=" + eventPhase + "]";
}
-
Thanks, I noticed that later :-)
jweeks123-> I actually solved it, and you weren't totally off!
Notice, that this class is used in another context than the previous. This is loading a queue of images instead of submitting a form.
I Changed my custom event class to this
Code:
package
{
import flash.events.Event;
public class imageQueueLoadedEvent extends Event
{
public static const ON_QUEUE_LOADED:String = "onQueueLoaded";
private var command:String;
public var params:Object;
public function imageQueueLoadedEvent(command:String, params:Object):void
{
super(ON_QUEUE_LOADED, bubbles, cancelable);
this.params = params;
this.command = command;
}
public override function clone():Event
{
return new imageQueueLoadedEvent(command, this.params);
}
}
}
Passing my arguments when dispatching the event
Code:
dispatchEvent(new imageQueueLoadedEvent("onQueueLoaded",imageArray));
And accessing the arguments with
Code:
private function onImageQueueLoadedHandler(e:imageQueueLoadedEvent):void
{
trace(e.params)
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|