Futurescale, Inc. PureMVC Home

The PureMVC Framework Code at the Speed of Thought


Over 10 years of community discussion and knowledge are maintained here as a read-only archive.

New discussions should be taken up in issues on the appropriate projects at https://github.com/PureMVC

Pages: [1]
Print
Author Topic: Mediators for views created at runtime  (Read 15333 times)
mycroft
Newbie
*
Posts: 7


View Profile Email
« on: November 18, 2007, 11:36:32 »

Hi,

I'm new with PureMVC and this forum, and this might've been asked before.

I have an app which creates the following hierarchy:

App -> Component with SuperTabNavigator -> [dynamic tab including other custom components]

The dynamic tabs could be a number of different views that are not related to each other and would need individual Mediators for each.

My question is how do I initialize Mediators for those dynamic components? From what i've seen with PureMVC you have to pass an instance of the views to the view mediator to initialize it. How would I do that with dynamic added views to another component (SuperTabNavigator uses addChild to add a new tab).

Your help would be greatly appreciated.

Regards,
Julian.
« Last Edit: November 18, 2007, 11:44:42 by mycroft » Logged
puremvc
Global Moderator
Hero Member
*****
Posts: 2871



View Profile WWW Email
« Reply #1 on: November 19, 2007, 09:29:06 »

Hi Julian,

There are a number of ways of dealing with deferred instantiation in the View. Here are a few ways, and the pros and cons of each.

1) Creating Mediators ahead of time with dummy View Components, then later passing the real View Components to them in Notification bodies.

Pros:
Easy because you can use a startup command to handle all the Mediator creation/registration at once, even though some of the View Components aren't created yet.

Cons:
Defeats the purpose of deferred instantiation, which is to avoid incurring the object creation cost until you need it.

Makes the Mediator have to work differently than other Mediators, because instead of setting listeners on the view component immediately in the constructor, we have to do it in handleNotification or a method invoked from within handleNotification.

2) Defer instantiation of the Mediator until its View Component is created. (Recommended). The View Component who will have children dynamically created at runtime must arrange a way to alert its Mediator of child creation. This can be done with a custom event class that the Mediator listens for and which has a property that is a reference to the new child. Or you can send a regular event that alerts the Mediator that it should check a 'lastChildCreated' property that would hold a reference to this newly created child (hint:bind to the tab navigator's selectedItem property). When the Mediator hears this event, it gets the reference to the new child and based upon its id property or some other discriminator, determines the appropriate Mediator to instantiate and register. If the process of determining which Mediator to create is very involved, you might want to move this part of the process out to a Command, and have the Mediator respond to the event at the view simply sending a notification with the new View Component as the body and let the Command figure it out.

Pros:
Defers instantiation of the Mediator until it is required, meaning faster startup and lower memory footprint.

Cons:
More involved from an 'understanding the system' perspective. Deferred instantiation is automagical in Flex, we don't have to do anything to reap its benefits. But on a framework that manages a running Flex UI, we do have to think about it a bit and understand what's happening.

3) Turn off deferred instantiation for these children altogether. In the case of a normal ViewStack, TabNavigator or Accordion you can simply set creationPolicy="all'. This means that all the children will be created at startup, nothing will be deferred. (usually not recommended)

Pros:
If you have a very simple interface where the creation time isn't greatly affected by creating all the children to begin with, then this can remove the issue of dealing with deferred instantiation from the PureMVC side of things altogether. You create all Mediators to start with because all the children are already there.

Cons:
This can impact startup performance and memory footprint. It should only be done if you are sure that all the children will be used during the session, and the startup time is acceptable.

Purists will laugh at you and malign your ways.

Hope this helps,

-=Cliff>

Logged
mycroft
Newbie
*
Posts: 7


View Profile Email
« Reply #2 on: November 19, 2007, 01:58:14 »

Thank you Cliff..That makes sense!
Logged
shauno


Email
« Reply #3 on: November 20, 2007, 08:04:28 »

Purists will laugh at you and malign your ways.

hahahah!! I think I shall dedicate a portion of each day to laughing at "purists"!! ;)
Logged
dbasch
Jr. Member
**
Posts: 17


View Profile Email
« Reply #4 on: May 18, 2008, 01:19:22 »

This is very good info regarding WHERE to instantiate dynamic proxies as well. I was confused about whether instantiating mediators within another mediator was proper. Thanks Cliff.

Derek Basch
Logged
Pages: [1]
Print