mapping views to mediators at runtime

Tim vd Eijnden's Avatar

Tim vd Eijnden

08 Dec, 2009 09:32 AM

I'm developing a big flash website using FTD. We have a assets.swf which contains assets, some of these assets should be mapped to a mediator. All these assets are placed inside a mainScene Movieclip which is added to the contextview as soon as it's loaded.

Examples of MovieClips inside the assets.swf:
screen_tips | Export class: screen_tips | Base class: ScreenObject
screen_spots | Export class: screen_spots | Base class: ScreenObject
screen_agenda | Export class: screen_agenda | Base class: ScreenObject

I want to map all the ScreenObjects (extends MovieClip) to the Screenmediator.
At this moment I walk through the children inside the mainScene and check if it's a ScreenObject.

protected function forEachScreen(item : ScreenObject, index : int, vector : Vector.<ScreenObject>) : void
{
    if(!mediatorMap.hasMediatorForView(item)) mediatorMap.mapView(item, ScreenMediator, MovieClip);
}

This is working fine.

I also have windows which are used inside multiple screens, these windows have the Base class: WindowObject (extends MovieClip).

protected function forEachWindow(item : WindowObject, index : int, vector : Vector.<WindowObject>) : void
{   
    if(!mediatorMap.hasMediatorForView(item)) mediatorMap.mapView(item, WindowMediator, MovieClip);         
}

This is working till a certain point: if a window has already been mapped and mapview is called again the following exception is thrown:
Exception fault: Error: Mediator Class has already been mapped to a View Class in this context - [class WindowMediator] This happens while hasMediatorForView is false? Maybe because of a new instance?

What is the best way to do this? Am I doing this right or is this Bad Practice?

  1. Support Staff 1 Posted by Shaun Smith on 11 Dec, 2009 02:52 PM

    Shaun Smith's Avatar

    Hi Tim,

    Sorry for the delay. The reason it's not working is that the first argument to mediatorMap.mapView() needs to be a class (not an instance). And that class needs to be the concrete class of the instance that lands on stage - ie, screen_tips (which is a class, yes?) not ScreenObject (ie, not an abstract class or interface).

    The MediatorMap watches for instances of the view classes you have mapped to arrive on stage. When such an instance arrives, it creates a mediator for it and passes a reference of the view instance to that mediator. For example:

    mediatorMap.mapView( MyComponentClass, MyMediator );

    When an instance of MyComponentClass lands on stage, a new MyMediator instance will be created. If MyMediator wants a reference to the view that created it, you do this in the Mediator class:

    [Inject] public var component:MyComponentClass;

    Unfortunately, for performance reasons, you have to map by concrete types.

    Does that help to clear things up a little? Or have I missed the point of what you are wanting to do?

  2. 2 Posted by Tim vd Eijnden on 14 Dec, 2009 10:30 AM

    Tim vd Eijnden's Avatar

    Thanks for your reply. I've solved my problem by creating an array with all the class which needed to be mapped to the ScreenMediator, and did the same for the WindowMediator.

    After loading the assets the classes are mapped to the right mediators, then the movieclip containing the views (that are now mapped) are added to the stage.

  3. Till Schneidereit closed this discussion on 15 Dec, 2009 06:35 PM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac