All this said... there are hacky ways to visually emulate it... It's
effectively the same as using separate methods, but visually looks like what
you're after. There are some scope caveats to it ("this" references the
global scope rather than the current class, duplicate named variables are
overwritten to local scope), and potentially some memory clean up issues
(anonymous functions are tricky to GC).

========================================================================
<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml";
layout="absolute"
    dragEnter="dragEntered(event)" dragDrop="dragDropped(event)">
    <mx:Script>
        <![CDATA[
            import mx.events.CloseEvent;
            import mx.controls.Alert;
            import mx.core.DragSource;
            import mx.core.IUIComponent;
            import mx.managers.DragManager;
            import mx.events.DragEvent;

            private function dragEntered(event:DragEvent):void
            {
                var formatName:String =
Object(event.dragInitiator).toString();
                if(event.dragSource.hasFormat(formatName))
                {

DragManager.acceptDragDrop(IUIComponent(event.currentTarget));
                }
            }

            private function dragDropped(event:DragEvent):void
            {
                // Executing the values here rather than referencing in
closure because the values of mouseX
                // will have changed by the time it's referenced (your mouse
position will have moved from over the
                // original target to over the YES button on the Alert box).
                var my_x:int = event.localX - event.dragInitiator.mouseX;
                var my_y:int = event.localY - event.dragInitiator.mouseY;

                Alert.show('Really drop here?', 'Hello!', Alert.YES +
Alert.NO, null, function(ev:CloseEvent):void{
                    if(ev.detail == Alert.YES)
                    {
                        // event is still in scope here
                        event.dragInitiator.x = my_x;
                        event.dragInitiator.y = my_y;
                    }
                });
            }

            private function
dragTarget_mouseDownHandler(event:MouseEvent):void
            {
                var dragInitiator:IUIComponent =
IUIComponent(event.currentTarget);
                var ds:DragSource = new DragSource();
                var formatName:String = event.currentTarget.toString();
                ds.addData(dragInitiator, formatName);
                DragManager.doDrag(dragInitiator, ds, event, dragInitiator);
            }

        ]]>
    </mx:Script>

    <mx:Button id="foo" label="I'm such a drag"
mouseDown="dragTarget_mouseDownHandler(event)"/>

</mx:WindowedApplication>
====================================================

So basically what you're doing here is using an inline anonymous function as
the close handler for the alert box. The trick as I tried to point out in
the code comment is referenced values that might change between the outer
procedure execution time and when the anonymous function is executed should
be initialized outside of the anonymous function. In this case, I need to
calculate the X/Y drop coords because by the time the close handler is
executed, my mouse position relative to the object I dropped will have
changed as I move to hit "YES", so I calculate the value at the time of the
drop and then reference that inside the closure.


Hope this helps.

Beau



On Thu, Aug 27, 2009 at 10:35 AM, Tracy Spratt <[email protected]> wrote:

>
>
>  Flex procedural code is essentially single threaded.  The loop will stop
> all other processing, the handler will never get called, and the loop will
> never stop.
>
>
>
> There is NO sleep or delay or pause or anything like that in Flex.  You
> must use events.
>
>
>
> Tracy Spratt,
>
> Lariat Services, development services available
>   ------------------------------
>
> *From:* [email protected] [mailto:[email protected]] *On
> Behalf Of *Nick Middleweek
> *Sent:* Thursday, August 27, 2009 6:43 AM
> *To:* [email protected]
> *Subject:* Re: [Spam] RE: [Spam] [flexcoders] Question on Flex Script
> Execution + Alert.show
>
>
>
>
>
> Why won't it work? I can't see why it wouldn't... I'm still learning Flex
> so perhaps I've overlooked something.
>
> I do agree, it is bad coding but I can't see why it wouldn't work.
>
> The boolAlertContinue variable is in affect acting like semaphore...
>
>
> Cheers,
> Nick
>
>
>
>  2009/8/27 Tracy Spratt <[email protected]>
>
>
>
> No, no, no, this will not work.  You must use the event mechanism.
>
>
>
> Tracy Spratt,
>
> Lariat Services, development services available
>   ------------------------------
>
> *From:* [email protected] [mailto:[email protected]] *On
> Behalf Of *Nick Middleweek
> *Sent:* Wednesday, August 26, 2009 7:33 PM
> *To:* [email protected]
> *Subject:* Re: [Spam] [flexcoders] Question on Flex Script Execution +
> Alert.show
>
>
>
>
>
> I'm not sure if this technique is frowned upon but...
>
>
> You could have a new private var boolAlertContinue:Boolean = false;
>
> In your alertHandler function, you would set it to true... boolAlertContinue
> = true;
>
> And after the Alert.show and before the if(myAlert == 1) you need to do a
> do while loop...
>
> do {
>
>
>   //Not sure if there's a 'dummy' command to prevent CPU hog so we'll just 
> check the time...
>
>
>
>
>
>   var dtmNow:Date = new Date();
>
>
> }
>
>
> while (boolAlertContinue);
>
>
> or you could probably initialise your myAlert:int = null and in the do ...
> while loop check for (myAlert != null)
>
>
> Cheers,
> Nick
>
>
>  2009/8/26 Angelo Anolin <[email protected]>
>
>
>
> Hi FlexCoders,
>
> This has puzzled me a bit..
>
> I have the following scripts in my application:
>
> private var myAlert:int;
>
> private function testFunction(evt:Event):void
> {
>   Alert.show('do you want to proceed', 'Title', Alert.YES | Alert.NO, null,
> alertHandler, null, Alert.YES);
>
>   if(myAlert == 1)
>   {
>     // Do Something here
>   }
>   else
>   {
>     // Do other thing here
>   }
> }
>
> Private function alertHandler(evt:CloseEvent)
> {
>   if(evt.Detail == Alert.YES)
>   {
>     myAlert = 1;
>   }
>   else
>   {
>     myAlert = -1;
>   }
> }
>
> Now, what puzzles me is that the script after the Alert.show is triggered,
> the scripts following it are also triggered.
>
> Is there a way to ensure that the script following the Alert.show alert box
> would only be executed after the Yes or No buttons has been pressed?
>
> I won't be able to move the scripts after the Alert.show script to the
> alertHandler event since there are some objects being set / modified prior
> to the alert being called.
>
> Inputs highly appreciated.
>
> Thanks.
>
>
>
>
>   
>



-- 
Beau D. Scott
Software Engineer

Reply via email to