Mediator onRegister called more than once, with view's "excludeFrom".

Quentin's Avatar

Quentin

21 Oct, 2010 01:10 PM

Hey there!
I just found something odd.

I have views (those views have mediators) and they are sub-view (components) in a super-view (the app).
When I set the excludeFrom property on them and toggle their presence (off and on) by changing the app's currentState, their onRegister method gets called each time they're displayed.
I can understand that since (I think) the onRegister method is called when the view is added to the stage... Shouldn't that happen only once?

How could I effectively check and not execute (return) the onRegister method? Or is that a bug?

Right now I'm using "hasEventListener" to check if my view already has the event listeners, and if so, I simply return. But that looks ugly, right?

Got tips?
Thanks!

  1. Support Staff 1 Posted by Stray on 21 Oct, 2010 01:33 PM

    Stray's Avatar

    I'm an AS3 rather than flex user, but the excludeFrom / includeIn Flex stuff refires the addedToStage event that triggers the Mediator to run onRegister();

    So... you might need to manually mediate, and not autoCreate/autoRemove by overriding the defaults in your mediatorMap.mapView:

    public function mapView(viewClassOrName:*, mediatorClass:Class, injectViewAs:* = null, autoCreate:Boolean = true, autoRemove:Boolean = true)

    However, in many cases it doesn't matter that the onRegister runs again, because you want your mediator to live and die with your view (when you excludeFrom it should be removed/cleaned up). It rather depends what's happening in onRegister. Usually all that is happening is that you're listening for events on the view (or signals) and on the central eventDispatcher.

    Hopefully that helps you figure out which (automatic or manual) is going to suit you.

    Stray

  2. 2 Posted by Quentin on 21 Oct, 2010 02:14 PM

    Quentin's Avatar

    Sweeet!
    I had to find where to put those mediatorMap.createMediator calls (I chose to put those calls in my main Mediator's onRegister) and how to sort them but that's it, easy.

    On a side-note: don't give the id "statusBar" to any of your components in your Flex project, you will end up searching for a bug that doesn't even exist...

    Thanks!

  3. Quentin closed this discussion on 21 Oct, 2010 02:14 PM.

  4. Quentin re-opened this discussion on 16 Nov, 2010 03:56 PM

  5. 3 Posted by Quentin on 16 Nov, 2010 03:56 PM

    Quentin's Avatar

    Wouldn't you recommend overriding onRemove instead of manually mediating those views?
    It looks much cleaner to simply .mapView(ViewClass, ViewClassMediator); and add event listeners in the mediator's onRegister and then remove them in its onRemove, or at least to me.

    Thoughts?

  6. Support Staff 4 Posted by Stray on 29 Nov, 2010 03:17 PM

    Stray's Avatar

    Hi Quentin -

    as I said, it all depends on what happens in your onRegister / onRemove.

    The possible difficulty I can see is that if you use the automatic mapping then the mediator is being created / destroyed each time. Therefore it has no state - so you can't necessarily rely on that to decide whether to rerun your onRegister code.

    You also end up with logic in your mediator ... which some of us think is a very bad thing, and other people don't worry about - it all depends whether you're trying to follow the mediator pattern or the presentation pattern. In the later case the mediator becomes a kind of controller for your view.

    Mediators and View controllers are an area of healthy debate in RL :)

    So - if you have a purist kind of mediator, there's no need to do anything. If you have a view-controller kind of mediator, you'd probably want to switch to manual mediation to avoid re-running expensive code or putting brittle conditionals into every mediator.

    I'm not sure I've said anything different here though... ? Hmm... maybe you had a different question?

  7. 5 Posted by Quentin on 29 Nov, 2010 03:37 PM

    Quentin's Avatar

    Thanks, yes that makes sense... Your answer is what I was looking for: no unique way of thinking!

    Since my views are supposed to be added and removed from the stage but not very frequently I think I should stick with the onRegister+onRemove logic, looks a look cleaner to me.

    Thanks again!

  8. Stray closed this discussion on 11 Feb, 2011 10:38 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