Thanks Alex, the runUpdate is a special case for the current use, its not
for *general use* and it would come out if/when I would share this
component.

I originally had the timerTimedOut not attached to the timer itself and I
was trying to track down an issue and attaching it to the timer was part of
that process.

Thanks again. This is my first, what I consider *real* component so I am
learning as I go. I made the changes you described and the *light bulb* has
shown.

Thanks Alex.

On 30 Mar 2007 10:14:07 -0700, Alex Harui <[EMAIL PROTECTED]> wrote:

   Clint,

I didn't bother to run this, so I could be wrong.

First, an unsolicited comment: If I were in your shoes I would extend
UIComponent and have a child Label or UITextField.  I don't think you want
to extend Text because to me this isn't a  Text component.  Your
CountDownTImer probably doesn't need to support htmlText, multiline text,
etc.

Now to your question.  IMHO, you want to dispatch "timerTimedOut" from the
component and not the internal timer, so it would be

public function timerEnd(event:TimerEvent):void{
trace("timer ended");
if(minutes == 0 && seconds == 0){
text = "0:00";
dispatchEvent(new Event("timerTimedOut"));
return;
}
startTimer();
}
Then you need metadata on the component to advertise that you dispatch
such an event when done.  That would look like this:

package
{
import mx.controls.Text;
import flash.utils.Timer;
import flash.events.TimerEvent;
import mx.utils.ObjectUtil;
import flash.events.Event;

/**
 *  Dispatched when the content is scrolled.
 *
 */
[Event(name="timerTimedOut", type="flash.events.Event")]
public class CountDownTimer extends Text{
public var minutes:Number=1;
public var seconds:Number=59;
Finally, you would no longer need to addEventListener("timerTimedOut"...)
in startTimer as you should simply call timedOutListener before you dispatch
the event.  Once you are set up like this folks do not pass you a function
to call back, they simply add themselves as listeners for the event.  That
way more than one person can get called back via the event listeners and it
is one less thing to code.

To be really picky, I see a call to parentDocument.runUpdate.  That
requires that everyone who uses this component have such a callback.  It
would be much better to use a custom event there.

 ------------------------------
*From:* [email protected] [mailto:flexcompone
[EMAIL PROTECTED] *On Behalf Of *Clint Tredway
*Sent:* Friday, March 30, 2007 9:40 AM
*To:* [email protected]
*Subject:* [flexcomponents] passing a function into a component

 Hey all, I am stumped. I am trying to make my CountDownTimer component
as reusable as I can and I am stumped on how to pass a function
reference to it.

Here is what I want to do:

when the timer finally reaches the end, I want to fire a function that
is in the parent document so I can use my logic for when this event
occurs.

I have my custom event all registered and it fires when its supposed
to, but how the heck do I reference this outside function?

****************** CODE BELOW ********************************

package
{
import mx.controls.Text;
import flash.utils.Timer;
import flash.events.TimerEvent;
import mx.utils.ObjectUtil;
import flash.events.Event;

public class CountDownTimer extends Text{
public var minutes:Number=1;
public var seconds:Number=59;
public var timedOut:Function;
private var minuteDisplay:Number=minutes-1;
private var zero:String="";
private var newTimer:Timer;

public function CountDownTimer(){
super();
}

private function onTick(event:TimerEvent):String{
seconds--;
if(seconds < 10){
zero = "0";
} else {
zero = "";
}

if(seconds == 30 || seconds == 0){
var n:Number;
var m:Number;
if(seconds == 0){
n=59;
} else {
n=30;
}

if(minutes != 0 && seconds != 30){
m=minutes-1;
} else {
m=minutes;
}

parentDocument.runUpdate(minutes,seconds);
}

text = minutes + ":" + zero + seconds;
return text;
}

public function timerEnd(event:TimerEvent):void{
trace("timer ended");
if(minutes == 0 && seconds == 0){
text = "0:00";
newTimer.dispatchEvent(new Event("timerTimedOut"));
return;
}
startTimer();
}

public function startTimer():void{
if(seconds == 0){
seconds=59;
} else {
seconds = seconds;
}
minutes--;
newTimer = new Timer(1000,seconds);
newTimer.addEventListener(TimerEvent.TIMER_COMPLETE,timerEnd);
newTimer.addEventListener(TimerEvent.TIMER, onTick);
newTimer.addEventListener("timerTimedOut",timedOutListener);
newTimer.start();
trace("timer started");
}

public function stopTimer():void{
newTimer.stop();
trace("timer stopped");
}

private function timedOutListener(eventObj:Event):void{
trace("Timed out listener fired");
stopTimer();
//this is where I need to call the passed in function
//timedOut();
}
}
}

thanks for any help!
--
I am not a diabetic, I have diabetes
my blog - http://grumpee.instantspot.com/blog




--
I am not a diabetic, I have diabetes
my blog - http://grumpee.instantspot.com/blog

Reply via email to