Futurescale, Inc. PureMVC Home

The PureMVC Framework Code at the Speed of Thought


Over 10 years of community discussion and knowledge are maintained here as a read-only archive.

New discussions should be taken up in issues on the appropriate projects at https://github.com/PureMVC

Pages: [1]
Print
Author Topic: Event gets Dispatched, but Mediator Not recognizing the Event??  (Read 13026 times)
Westside
Jr. Member
**
Posts: 12


View Profile Email
« on: December 10, 2008, 10:32:17 »

Hi,

I have an event that gets dispatched from a "view" within a ViewStack upon creationComplete.  As part of my troubleshooting process I put a breakpoint to ensure that the event was in fact being dispatched and that works.  This mediator is mediating for a ViewStack.  When the first view in the ViewStack calls its "creationComplete" method I dispatch my event.

The problem is the callback function in the Mediator "never" gets called and I can't figure out why.  Any help highly appreciated.

In: MainDisplay.mxml

public static const GET_STATES:String = 'getStates';

private function loadStates(event:Event):void  {
     dispatchEvent( new Event( GET_STATES,true ) );
}

 <mx:ViewStack id="myStack"
     selectedIndex="{currentViewSelector}"
       creationPolicy="auto" 
       width="100%" height="100%"
    >
       
 <view:SplashView id="splashPage" showEffect="{fade_HomePage}" hideEffect="{zoom_HomePage}" creationComplete="loadStates(event)"/>

</mx:ViewStack>

In: MainDisplayMediator.as

override public function onRegister():void  {
  mainDisplay.addEventListener( MainDisplay.GET_STATES, cbLoadStates );
}

private function cbLoadStates ( event:Event ):void  {
    Alert.show('If this shows, then the call back function works');
   sendNotification( ApplicationFacade.LOAD_STATES );
         
}
« Last Edit: December 10, 2008, 10:33:53 by Westside » Logged
Joel Hooks
Courseware Beta
Sr. Member
***
Posts: 146


baby steps

 - 46288188  - passport@provinsal.com  - joeltuff
View Profile WWW Email
« Reply #1 on: December 11, 2008, 12:33:56 »

are you sure the creationComplete event is happening after the mediator has been created and registered?
Logged

http://joelhooks.com - my ramblings about developing with actionscript and python using pureMVC and django respectively.
puremvc
Global Moderator
Hero Member
*****
Posts: 2871



View Profile WWW Email
« Reply #2 on: December 11, 2008, 07:02:56 »

The first viewstack child is created as part of the process of creating MainDisplay. Therefore it is sending its creationComplete event before MainDisplayMediator exists or has a reference to MainDisplay.

As Steve Martin once said about comedy its all abot ti ming.

-=Cliff>
Logged
Jason MacDonald
Sr. Member
****
Posts: 243


View Profile Email
« Reply #3 on: December 11, 2008, 07:10:45 »

abot ti ming.

-=Cliff>

He must be Chinese? Never heard of Ti Ming ;)
Logged
Westside
Jr. Member
**
Posts: 12


View Profile Email
« Reply #4 on: December 11, 2008, 09:16:18 »

Hi,

What you guys are saying does make sense, but when I look at the rest of my code, it looks like the mediator is getting registered in the onRegister function.  Here is a code snippet:

In ApplicationMediator.as:

override public function onRegister():void   {
  facade.registerMediator(new MainDisplayMediator(app.mainDisplay));
}

protected function get app():MyApp  {
   return viewComponent as MyApp;
}

I am thinking you guys are right, but I am not sure where in the code I am going wrong.

-Westside
Logged
puremvc
Global Moderator
Hero Member
*****
Posts: 2871



View Profile WWW Email
« Reply #5 on: December 11, 2008, 12:41:26 »

Have a look at the Slacker demo to get an idea of how to handle this sort of scenario.
-=Cliff>
Logged
Westside
Jr. Member
**
Posts: 12


View Profile Email
« Reply #6 on: December 14, 2008, 10:46:53 »

Hi,

Actually my app is based off the Slacker demo.  In any case, I have re-downloaded the Slacker Demo and added minimal code to it to test what I am trying to do and the same thing happens.   The event does get dispatched but something is not right.  I have not changed any other code in the demo. The SplashView is the first view in ViewStack and it should be loaded automatically.  Here is what I added to the Slacker Demo:

MainDisplay.mxml

public static const LOAD_STATES:String = 'loadStates';
<view:SplashView creationComplete="sendEvent(LOAD_STATES)"  />

MainDisplayMediator.as

 override public function onRegister():void
        {
            mainDisplay.addEventListener( MainDisplay.GALLERY_CREATED, onGalleryCreated );
            mainDisplay.addEventListener( MainDisplay.EDITOR_CREATED, onEditorCreated  );
            mainDisplay.addEventListener( MainDisplay.PROFILE_CREATED, onProfileCreated );
            // my Event Listener
            mainDisplay.addEventListener( MainDisplay.LOAD_STATES, onLoadStates );
        }

// this is the function that never gets called for some reason
protected function onLoadStates( event:Event ):void
        {
           Alert.show('If you can read this, it worked');
   
        }

Any help appreciated.

-Westside
Logged
Westside
Jr. Member
**
Posts: 12


View Profile Email
« Reply #7 on: December 14, 2008, 09:55:48 »

Hi,

Well, I isolated the problem, but I am not sure how to fix it.   When I add a "creationComplete" event to the first child in the ViewStack (SplashView) it will dispatch the event but the listener that you add doesn't work.  It appears to only happen when I use a ViewStack and try to dispatch the event on "creationComplete".    So I added a button to the screen, then dispatched the same event that I am trying to via "creationComplete" and it works perfectly so I am at a loss on how to do this.

I would be very interested in how I can resolve this if possible, perhaps I am doing something just wrong, I am new to this so any help appreciated...

Thanks,

-
Westside
Logged
puremvc
Global Moderator
Hero Member
*****
Posts: 2871



View Profile WWW Email
« Reply #8 on: December 15, 2008, 07:56:08 »

Ok, sorry. I guess my earlier response didn't make sense.

Timing. Its about timing.

First of all, you need to understand the Flex creation process. It all happens BEFORE PureMVC startup begins.

For rendering the MXML application Flex does two passes at the first is a layout and measurment pass and goes from the outermost container (the app) th the innermost (the child of your viewstack for instance)

Then it knows where everything goes and how big to make it, so it does a second pass, this time from the innermost component to the outermost.

Each MXML component, including the app sends a creation complete when done, and so by the time the app itself sends creationComplete, all its rendered children have ALREADY sent their creationComplete events.

And since we don't call facade.startup until the app sends creationComplete, the mediator that listens for this viewstacks child to be created will never hear the event because it was sent before the mediator was created.

I hope this clears it up for you.

-=Cliff>
Logged
Westside
Jr. Member
**
Posts: 12


View Profile Email
« Reply #9 on: December 15, 2008, 06:43:43 »

Hi,

Thanks Cliff, yes this helped immensely. As you say the creationComplete was firing before the mediator was even registered which makes sense.  Sounds like I just need to do what I want to do during startup.

-Westside
Logged
puremvc
Global Moderator
Hero Member
*****
Posts: 2871



View Profile WWW Email
« Reply #10 on: December 15, 2008, 09:35:53 »

Yes. In the main mediator you can place the creationComplete listener and it will work when you navigate to the children whose instantiation has been deferred. But you need to also register the mediator for the first child explicitly since it already exists.

Or since that's kind of ugly (mediator assumes knowledge of view component's inner hierarchy to the first child).

You might consider having a non-mediated first child (splash). Then the user navigates to the second child at runtime, triggering its creationComplete, which the mediator hears and gets reference to the child to be mediated via the event, and matches it up with the right mediator.

-=Cliff>
Logged
Pages: [1]
Print