Map a Mediator to a Baseclass.

thomas.pujolle's Avatar

thomas.pujolle

23 Oct, 2012 01:29 PM

Hello guys!

I've just run into a problem as building my robotlegs application. I hope you'll be able to get me some help :) Let me explain.

I have a world map within an external swc, that contains all the countries in the world in specific movieclips. I need to be able to map each and every one of them to a common mediator, say "CountryMediator". My first idea was to create a baseclass that every movieclip extends, but nothing was mapped to these classes when they were added to the stage (and they surely were). I think it's kind of normal as the concrete class that gets exported every time is"France" "United Kingdom" "Spain", based on the class "Country".

Is there any workaround so I just have to write one mapping rule allowing me to map all the countries base on the Country class to the Country mediator?

Thanks for you help!

  1. Support Staff 1 Posted by Stray on 23 Oct, 2012 02:05 PM

    Stray's Avatar

    Hi Thomas,

    If you make sure that Country implements an interface, you can use the ViewInterfaceMediatorMap utility to have one rule manage all instances of Country.

    The util is here:

    https://github.com/piercer/robotlegs-extensions-ViewInterfaceMediat...

    hth, if you have any problems with it do come back,

    Stray

  2. Support Staff 2 Posted by Ondina D.F. on 23 Oct, 2012 02:08 PM

    Ondina D.F.'s Avatar

    Hi Thomas,

    Please take a look at the discussion and the utility I linked below, and if you can’t find an answer in there, come back with more questions:)

    http://knowledge.robotlegs.org/discussions/questions/290-mediating-...

    https://github.com/piercer/robotlegs-extensions-ViewInterfaceMediat...

    Cheers,
    Ondina

  3. 3 Posted by thomas.pujolle on 23 Oct, 2012 02:33 PM

    thomas.pujolle's Avatar

    Thanks for that ! i'm at this moment trying this ViewInterfaceMediator.
    I have 6 WorldZones in my swc, based on the class WorldZone which implements IWorldZone.

    But for my 6 views, I get 5 errors (after the first instantiation) saying:
    Warning: Injector already has a rule for type "com.worldmap.view::WorldZoneMediator", named "".
    If you have overwritten this mapping intentionally you can use "injector.unmap()" prior to your replacement mapping in order to avoid seeing this message.

    I have this in my mediator:
    [Inject] public var worldZone:IWorldZone;

    And this in my mediator map:
    mediatorMap.mapView(IWorldZone, WorldZoneMediator);

    Am I doing this right? It's weird because it works once, and then fires a warning.

  4. Support Staff 4 Posted by Ondina D.F. on 23 Oct, 2012 04:47 PM

    Ondina D.F.'s Avatar

    You’re getting those warnings because you’re (probably) using robotlegs 1.5+ and SwiftSuspenders 1.6.

    ViewInterfaceMediatorMap worked well with robotlegs v1.4, where robotlegs and swiftsuspenders were in sync.

    Stray had a patch for robotlegs’ Context.as, but I can’t find any swcs reflecting the changes.
    Try using Stray’s fork of robotlegs.

  5. Support Staff 5 Posted by Ondina D.F. on 23 Oct, 2012 04:52 PM

    Ondina D.F.'s Avatar
  6. 6 Posted by thomas.pujolle on 25 Oct, 2012 08:53 AM

    thomas.pujolle's Avatar

    Thanks a lot for you help!

    I've try to add the previous fixes to my code but it does not seem to help avoiding the warnings. I'm sorry my comprehension of RL is not good enough to allow me to fix this all by myself :/

    Do you think I may construct my context the wrong way? I have this:

    public class ApplicationContext extends InterfaceEnabledMediatorMapContext {

        public function ApplicationContext(contextView : DisplayObjectContainer) {
    
            super(contextView, true);
        }
    
        override public function startup() : void {
    
            new ModelsInitializer(injector);
            new ServicesInitializer(injector);
            new ApplicationStartupSequenceCommandsInitializer(commandMap);
            new ApplicationCommandsInitializer(commandMap);
            new ViewMediatorsInitializer(mediatorMap, contextView);
    
            super.startup();
        }       
    }
    

    And this:
    public class InterfaceEnabledMediatorMapContext extends Context {

        public function InterfaceEnabledMediatorMapContext(contextView : DisplayObjectContainer = null, autoStartup : Boolean = true) {
            super(contextView, autoStartup);
        }
    
        override protected function get mediatorMap() : IMediatorMap {
            return _mediatorMap ||= new ViewInterfaceMediatorMap(contextView, createChildInjector(), reflector);
        }
    
        override public function set contextView(value : DisplayObjectContainer) : void {
            if (_contextView != value) {
                _contextView = value;
                _injector.applicationDomain = getApplicationDomainFromContextView();
                _commandMap = null;
                _mediatorMap = null;
                _viewMap = null;
                unmapInjections();
                mapInjections();
                checkAutoStartup();
            }
        }
    
        protected function unmapInjections() : void {
            trace("InterfaceEnabledMediatorMapContext.as :: unmapInjections");
            injector.unmap(IReflector);
            injector.unmap(IInjector);
            injector.unmap(IEventDispatcher);
            injector.unmap(DisplayObjectContainer);
            injector.unmap(ICommandMap);
            injector.unmap(IMediatorMap);
            injector.unmap(IViewMap);
            injector.unmap(IEventMap);
        }
    }
    

    Thanks a lot!!!

  7. Support Staff 7 Posted by Ondina D.F. on 25 Oct, 2012 10:13 AM

    Ondina D.F.'s Avatar

    Hey Thomas,

    No problem:)

    It would be easier if you used robotlegs source and replaced Context.as with the patched one. It would work with SwiftSuspenders 1.6, too. I’ll attach rl source with those changes.
    Let us know if it worked.

    Ondina

  8. 8 Posted by thomas.pujolle on 25 Oct, 2012 10:31 AM

    thomas.pujolle's Avatar

    All right!

    So if I understand the .zip contains the RL sources with the patched Context.as, which will prevent me from getting stuff like:
    Warning: Injector already has a rule for type "com.worldmap.view::WorldZoneMediator", named "".
    If you have overwritten this mapping intentionally you can use "injector.unmap()" prior to your replacement mapping in order to avoid seeing this message.

    And I should just add SwfSuspender 1.6 at the top of this, right?

  9. Support Staff 9 Posted by Ondina D.F. on 25 Oct, 2012 10:33 AM

    Ondina D.F.'s Avatar

    Exactly.

  10. 10 Posted by thomas.pujolle on 25 Oct, 2012 10:36 AM

    thomas.pujolle's Avatar

    It does not seem to work :( I didn't find the required adapters in your .zip so I used so ones in the latest RL (1.5.2).

    What do you think could cause this? My context extends this one, which extend the one in your .zip:

    `public class InterfaceEnabledMediatorMapContext extends Context {
        public function InterfaceEnabledMediatorMapContext(contextView : DisplayObjectContainer = null, autoStartup : Boolean = true) {
            super(contextView, autoStartup);
        }
    
        override protected function get mediatorMap() : IMediatorMap {
            return _mediatorMap ||= new ViewInterfaceMediatorMap(contextView, createChildInjector(), reflector);
        }
    }`
    
  11. Support Staff 11 Posted by Ondina D.F. on 25 Oct, 2012 10:39 AM

    Ondina D.F.'s Avatar

    Have you deleted the rl swc and refreshed the project?

  12. 12 Posted by thomas.pujolle on 25 Oct, 2012 12:44 PM

    thomas.pujolle's Avatar

    I have.
    I have the swf suspender SWC + your .zip source code + the two adapters found in the latest robotlegs (master branch).

    Still something seems to be wrong :(

  13. 13 Posted by thomas.pujolle on 25 Oct, 2012 12:48 PM

    thomas.pujolle's Avatar

    I need to tell my SWF works perfectly anyway, I just wonder if it's going to impact performances, maybe not?

  14. Support Staff 14 Posted by Ondina D.F. on 25 Oct, 2012 01:10 PM

    Ondina D.F.'s Avatar

    One moment, please. I'm about to answer.

  15. Support Staff 15 Posted by Ondina D.F. on 25 Oct, 2012 01:41 PM

    Ondina D.F.'s Avatar

    I’m back.

    I guess it’s because you’re initializing your context in actionscript.

    Try initializing your context inside of the declaration tag, like in piercer’s example:
    <fx:Declarations>
    <mvcs:InterfaceEnabledMediatorMapContextExample contextView="{this}"/>
    </fx:Declarations>

    See if this works with the patched Context. If it doesn’t work either, then maybe I gave you the wrong patch. I don’t know whether there is a newer version or another solution.
    Stray would know more about this.

    The warnings aren’t that important, and won’t affect your app’s performance, so, I guess, you could live with it;)

  16. 16 Posted by thomas.pujolle on 25 Oct, 2012 01:51 PM

    thomas.pujolle's Avatar

    I'm not using flex so I won't be able to try this out, but I think I'm going to stand by what I have right now if you say it does not impact the application, thanks anyway for your time :) RL rules!

  17. Support Staff 17 Posted by Ondina D.F. on 25 Oct, 2012 02:28 PM

    Ondina D.F.'s Avatar

    No problem, Thomas.
    I’ll close this discussion for now. You can re-open it, if need be.

    Cheers,
    Ondina

  18. Ondina D.F. closed this discussion on 25 Oct, 2012 02:28 PM.

  19. thomas.pujolle re-opened this discussion on 12 Nov, 2012 02:18 PM

  20. 18 Posted by thomas.pujolle on 12 Nov, 2012 02:18 PM

    thomas.pujolle's Avatar

    Hello guys,

    I think I'll have to get through this another time, I have issues when deleting views.
    No errors thrown but the flow gets broken at some point, after a mediator deletion. After that, no other mediator of this type is created.

    Has anyone in one of his project a version of RL core + the context fix + ViewInterfaceMediatorMap working?

    Otherwise I'll have to create this by myself, although I didn't get it right last time.

    Thanks for you help :)

  21. Support Staff 19 Posted by Ondina D.F. on 14 Nov, 2012 04:40 PM

    Ondina D.F.'s Avatar

    Sorry, that you didn't get any answers. I don't know how to help you.

  22. Support Staff 20 Posted by Ondina D.F. on 11 Dec, 2012 11:51 AM

    Ondina D.F.'s Avatar

    Hi Thomas,

    Have you found a solution?
    If not, have you considered using robotlegs 2? I don’t know if you can afford to migrate your project to rl2, but rl2 offers easier and cleaner solutions for your use case. Just saying:)

    Ondina
    P.S. As you know, you can reopen the discussion, if you want to answer

  23. Ondina D.F. closed this discussion on 11 Dec, 2012 11:51 AM.

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