How to access the component which is not on the stage.

Flex Incubator's Avatar

Flex Incubator

15 Nov, 2011 08:14 PM

There are number of components in the app which states should be reset in particular situation (for example, when the user log out from the app). The resetting consists of removing some nested components. I can not access these components via mediators because they are not on the stage. The only way I can access them is via some parent container. For example, this parent container may catch the event via mediator and call reset() methods in nested components. But this is what I am trying to avoid. So, is there any way to access the components via mediator when it's not on the stage?

Thanks for help.

Ilya

  1. 2 Posted by Daganev on 15 Nov, 2011 08:34 PM

    Daganev's Avatar

    Hi Ilya,
    I think in your case you want to use Commands and not Mediators. Then in the commands, Inject the non display list objects to be operated upon.

  2. 3 Posted by Flex Incubator on 15 Nov, 2011 08:43 PM

    Flex Incubator's Avatar

    Well, they are display objects, and actually they have their mediators that works perfectly, but obviously, only when their views are on the stage.

  3. 4 Posted by Stray on 15 Nov, 2011 08:47 PM

    Stray's Avatar

    If you set autoremove to false when you map the mediators then they will continue to be mediated even while they aren't on the stage.

    Or you could use the relaxed eventMap utility (search on github) which lets them catch up as soon as they do land on the stage.

    hth,

    Stray

  4. 5 Posted by Pierre Laveklint on 15 Nov, 2011 08:50 PM

    Pierre Laveklint's Avatar

    How about doing the reset just as they left the stage by overriding the "onRemove" in their Mediators, where you call something like:

    override public function onRemove():void
    {
        view.reset(); 
    }
    
  5. 6 Posted by Flex Incubator on 15 Nov, 2011 08:58 PM

    Flex Incubator's Avatar

    @Pierre Laveklint this will not work in my case, because some views are not on the stage on the moment when the reset should be done

    @Stray I think relaxed eventMap will not work for me as well. The problem is that the user may notice some "blinking" when the components are removed from stage when he/she logs in again. I was thinking about not removing of the mediator, but this also means that I will have to check several other things. The thing is that some of my views may accept the same event. It's not a problem in normal workflow, because they are never placed on the stage at the same time thus they are never mediated at the same time. I wonder whether I can create 2 different mediators for the same view. Then the second mediator will never be deleted and I will only put reset handler there. Is it normal practice, or is it something completely weird?

  6. 7 Posted by Flex Incubator on 15 Nov, 2011 09:05 PM

    Flex Incubator's Avatar

    Another idea has just came to my mind and I wonder what you guys think about it. So, I have reset function in several views that should be reset at some point of workflow. What if I create some ResetModel. This model can hold callbacks. When the view is created, the reference to the reset function will be send to the ResetModel (via some Command). This way, i will not care if the view that should be reset is on the stage, and I also do not need to hold mediators when they are not needed.

  7. 8 Posted by Stray on 15 Nov, 2011 09:05 PM

    Stray's Avatar

    You can manually create extra mediators - all you need to do is not put an inject tag on the view, use the injector to get a new mediator instance, and then set the view manually. Then run setViewComponent(view) on the mediator, and then preRegister().

    How big a project is it? I've got the Robotlegs 2 mediator map working with Robotlegs 1 mediators ... so in theory you could use that - which also allows you to create multiple mediators per view.

    It's still alpha, but if your project is more than another week or so long then that might be as good a solution.

    A final option would be to wire a signal direct into your view components - and use a command to update those signals.

    Stray

  8. 9 Posted by Flex Incubator on 15 Nov, 2011 09:10 PM

    Flex Incubator's Avatar

    The project are going to continue for another several weeks, but as long as this is phase 1 of it, which will be released very soon, I will go with robotlegs 1 for now.
    I have not drilled into Signals yet, heard a lot of good stuff about them. Hope will try them in my next project.

  9. 10 Posted by Flex Incubator on 15 Nov, 2011 09:12 PM

    Flex Incubator's Avatar

    Stray, could you please describe your first paragraph in more details? Where exactly should I create this new mediator, where I will be able to remove it when it's no longer needed. Maybe some small code snippet? :)

  10. 11 Posted by Stray on 15 Nov, 2011 10:22 PM

    Stray's Avatar

    Where you should create it is the tricky one.

    I'd have a manual mediator factory - and inject that into your normal mediators (the ones for these views).

    Then in the mediator, onRegister, you just do something like:

    mediatorFactory.createMediator(OtherMediator, view);

    The factory is a singleton.

    It needs the injector injected into it.

    It looks something like:

    [Inject]
    public var injector:IInjector;

    private const _mediatorsByView:Dictionary = new Dictionary();

    public function createMediator(mediatorClass:Class, view:DisplayObject):void
    {
    // check if one already exists, and exit if it does
    if(_mediatorsByView[view])
    return;

    var mediatorInstance:IMediator = new mediatorClass();
    injector.injectInto(mediatorInstance);
    mediatorInstance.view = view;
    mediatorInstance.setViewComponent( view );
    mediatorInstance.preRegister();
    _mediatorsByView[view] = mediatorInstance;
    }

    public function destroyMediatorForView(view):void
    {
    if(!_mediatorsByView[view])
    return;

    var mediatorInstance:IMediator = _mediatorsByView[view];
    mediatorInstance.preDestroy();
    delete _mediatorsByView[view];

    }

    The first time the createMediator code runs (the first time the view is on stage), it creates the new mediator. After that it just exits.

    I'm not sure how you'd know when it's not needed any more, but you could possibly do that using an event, and again do it via the primary mediator.

    Let me know if this still isn't clear enough - late here!

    Stray

  11. 12 Posted by Flex Incubator on 15 Nov, 2011 10:40 PM

    Flex Incubator's Avatar

    Thanks, Stray for your help and examples! It's clear now. Could you please share your thoughts about my idea of creating ResetModel, which will hold callback functions from view (reset functions in my particular case).

    Ilya

  12. Ondina D.F. closed this discussion on 31 Dec, 2011 09:45 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