Failing to Map Mediator

codecraig's Avatar

codecraig

06 Jan, 2010 04:32 PM

I have a Flex application and in my view I do this:

protected function onPreInitialize():void {
        removeEventListener(FlexEvent.PREINITIALIZE, onPreInitialize);
        context = new MyContext(this);
}

In MyContext I do this:

override public function startup():void {
    mediatorMap.mapView(ViewA, ViewAMediator);
}

I stepped in with the debugger and inside the MediatorMap.mapView method I got the following "if" test

if (autoCreate && contextView && (viewClassName == getQualifiedClassName(contextView) ))
{
  createMediator(contextView);
}

The last test in that "if" (i.e. viewClassName == getQualifiedClassName(contextView)) seems to be false. My view class name is something like: com.acme.ViewA and contextView seems to reference my "main" class (i.e. the one that extends Module, com.acme.MainModule).

So in MyContext after I call "mapView" I now invoke mediatorMap.createMediator(ViewA), however it fails with:

[Fault] exception, information=TypeError: Error #1034: Type Coercion failed: cannot convert     com.acme::ViewA$ to com.acme.ViewA.

It is failing in MediatorMap.createMediator at the line that does:

mediator = injector.instantiate(config.mediatorClass);

Any ideas what I'm doing wrong?

  1. Support Staff 1 Posted by Till Schneidere... on 06 Jan, 2010 04:47 PM

    Till Schneidereit's Avatar

    Hey Craig,

    the piece of code you stepped to is just a special case handling that
    enables seamless mediation of the context view itself. I guess your
    ViewA isn't the context view but some view inside that, correct?
    If so, then you can ignore this part - the if statement should fail as it does.

    As for the error you get:
    Robotlegs works by automatically creating instances of the mediator
    class you mapped for your view class as soon as an instance of the
    latter gets added to the display list anywhere below your context
    view.
    It looks like you're trying to not only create the mediator but also
    the view itself with the line
    mediatorMap.createMediator(ViewA)
    The error you get is caused by Robotlegs trying to inject what you
    supplied to createMediator - the *class* ViewA - into a variable on
    your mediator that expects something different - namely *an instance*
    of the class ViewA.

    Instead, you can (after creating the mapping as you now do in
    MyContext#startup) add an instance of ViewA to the context view (or
    one of its children) and the mediator gets created automatically.

    The smallest change to your code that should create working results would be:
       override public function startup():void {
           mediatorMap.mapView(ViewA, ViewAMediator);
           contextView.addChild(new ViewA());
       }

    Note that you wouldn't normally create view instances in your
    Context#startup method - this is just to demonstrate that views get
    mediated automatically after being added to the stage.

    cheers,
    till

  2. 2 Posted by codecraig on 06 Jan, 2010 04:53 PM

    codecraig's Avatar

    Nevermind, apparently I was trying to create my context after ADDED_TO_STAGE
    occurred, I thought it was in the onPreInitialize but it wasn't, now it is.

    @till -- thanks for the heads up / info so quickly

  3. Till Schneidereit closed this discussion on 06 Jan, 2010 05:07 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