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: Managing a lot of Proxys and Mediators  (Read 15775 times)
pirol
Newbie
*
Posts: 7


View Profile Email
« on: December 03, 2009, 04:20:43 »

Hi,
My problem is very simple but i don't know how i should manage many Proxys with many Mediators.

The app should load images. The paths of these images are in a xml file.
Know I thought about the situation and get that conclusion:

XMLProxy loading XML (finished) -> ImageLoadCommand -> ImageProxy (for each Image) -> ImageVo (SaveData) -> ?

Is that logic right? And how can I send a Notification to a ImageMediator which was created after the XML had been loaded? How should I mange the Names of ImageProxys and ImageMediators

Tobi

Sry for the bad english :D
Logged
puremvc
Global Moderator
Hero Member
*****
Posts: 2871



View Profile WWW Email
« Reply #1 on: December 03, 2009, 05:59:23 »

Well you might not want a separate proxy for each image. You might want an ImageProxy that has a collection of images.

Also, for this you might want to look into the LoadupUtility which handles loading all sorts of stuff and deals with load order dependencies and retry, etc.

-=Cliff>
Logged
jpwrunyan
Sr. Member
****
Posts: 84


View Profile WWW Email
« Reply #2 on: December 03, 2009, 06:20:15 »

XMLProxy loading XML (finished) -> ImageLoadCommand -> ImageProxy (for each Image) -> ImageVo (SaveData) -> ?

Is that logic right? And how can I send a Notification to a ImageMediator which was created after the XML had been loaded? How should I mange the Names of ImageProxys and ImageMediators

I had the same exact question.  I think you are fine.  As far as the last step, here's what I do in my Proxies.  I dispatch a notification inside the setData() function something like this:

:
override public function setData(data:Object):void {
  super.setData(data);
  sendNotification(GlobalConstants.NEW_DATA, data, proxyName);
}

Anyway, your mediators might all respond to the NEW_DATA notification, then check if the type (ie proxyName) matches a proxy name they care about, and then finally process the data appropriately.

Finally, if your Proxy class is basically a single-instance class, then I would just make a specific notification name for it and not use the "type" parameter above.  In reality, this is what I do most of the time.  It cuts out a lot of needless handleNotification() calls that are ultimately ignored inside the unrelated Mediators anyway, but at the expense of having more notification names declared in your application.

The first approach is medium-simple, code-consistent, but redundant/wasteful, the second approach is very simple, efficient, but has higher code maintenance and documentation time.

I'm still looking for a silver bullet.
Logged
puremvc
Global Moderator
Hero Member
*****
Posts: 2871



View Profile WWW Email
« Reply #3 on: December 04, 2009, 09:11:57 »

Anyway, your mediators might all respond to the NEW_DATA notification, then check if the type (ie proxyName) matches a proxy name they care about, and then finally process the data appropriately.

Every Mediator listening for every Proxy's data to be set change makes for a very chatty application, lots of wasted cycles spent inspecting then ignoring things. Reading the code will also be more difficult, since everything is responding to generic note names and then trying to work out what they mean.

Much better to work out your communication protocol (specific notification names or at least rules for their naming, data carried, interested observers and their responses) early on in your design. This will allow you to streamline your internal communications so that your application operates more efficiently as well as making the code much clearer, since semantically-laden notification names are far easier to follow than generic catch-all names.

-=Cliff>
Logged
pirol
Newbie
*
Posts: 7


View Profile Email
« Reply #4 on: December 09, 2009, 03:50:55 »

Thx for the answers

I have done it with the Loadup Utility...if you know how to use it, the loading of assets is realy easy.

Now, i have one GalleryMediator which controls all images. Each of these images has his own Mediator (ImageMediator).
Is this technique efficient or should i use the GalleryMediator for all images?
The images have the same behaviour as button (hover-effect etc)
Logged
hesten
Sr. Member
****
Posts: 52


View Profile Email
« Reply #5 on: December 10, 2009, 05:22:14 »

If the application is a gallery, I think I would stick with a component that displays thumbnails in whatever way, and let that have a meditor that plugs it into the system.

I would not have a mediator for each thumb since these would be a natural subcomponent of a thumbnail list display. I see no need for a bunch of thumb mediators. Treat them as subcomponents of a list display and communicate through events (which you can just bubble up to the GalleryMediator).

On the other hand I would have a ImageDisplay with its own mediator that recieves notifications from a GalleryMediator (probably passing a ImageVO). Then the two displays are completely seperated which makes sense in my opinion.


Logged
pirol
Newbie
*
Posts: 7


View Profile Email
« Reply #6 on: December 10, 2009, 08:09:30 »

Ok thx... now the next question :D

I have a StartupCommand which initialize a StyleSheet and a SiteDataProxy:
:
public class StartupCommand extends SimpleCommand implements ICommand{
private var _monitor : LoadupMonitorProxy;

override public function execute(note : INotification):void{
var stage : Stage = note.getBody() as Stage;
facade.registerMediator(new StageMediator(stage));

facade.registerProxy(new LoadupMonitorProxy());
_monitor = facade.retrieveProxy(LoadupMonitorProxy.NAME) as LoadupMonitorProxy;

var styleSheetProxy : ILoadupProxy = new StyleSheetProxy();
var siteDataProxy : ILoadupProxy = new SiteDataProxy();

facade.registerProxy(styleSheetProxy);
facade.registerProxy(siteDataProxy);

var rStyleSheetProxy : LoadupResourceProxy = makeAndRegisterStartupResource(StyleSheetProxy.SRNAME, styleSheetProxy);
var rSiteDataProxy : LoadupResourceProxy = makeAndRegisterStartupResource(SiteDataProxy.SRNAME, siteDataProxy);

rSiteDataProxy.requires = [rStyleSheetProxy];
_monitor.loadResources();
}

private function makeAndRegisterStartupResource(proxyName : String, appresourceProxy : ILoadupProxy) : LoadupResourceProxy {
var r : LoadupResourceProxy = new LoadupResourceProxy(proxyName, appresourceProxy);
facade.registerProxy(r);
_monitor.addResource(r);
return r;
}
}

Then i have a StageMediator:

:
public class StageMediator extends Mediator implements IMediator {
public static const NAME : String = "stageMediator";

public function StageMediator(viewComponent : Object = null) {
super(NAME, viewComponent);
}

override public function listNotificationInterests() : Array {
return[LoadupMonitorProxy.LOADING_PROGRESS,
LoadupMonitorProxy.LOAD_RESOURCE_TIMED_OUT,
LoadupMonitorProxy.LOADING_FINISHED_INCOMPLETE,
LoadupMonitorProxy.CALL_OUT_OF_SYNC_IGNORED,
LoadupMonitorProxy.LOADING_COMPLETE,
ApplicationFacade.STYLE_SHEET_FAILED,
ApplicationFacade.STYLE_SHEET_LOADED,
ApplicationFacade.STYLE_SHEET_LOADING,
ApplicationFacade.SITE_DATA_FAILED,
ApplicationFacade.SITE_DATA_LOADED,
ApplicationFacade.SITE_DATA_LOADING];
}

override public function handleNotification(note : INotification):void{
switch(note.getName()){
case  ApplicationFacade.STYLE_SHEET_LOADING:
trace("Loading StyleSheet...");
break;
case ApplicationFacade.STYLE_SHEET_LOADED:
trace("StyleSheetLoaded");
break;
case ApplicationFacade.SITE_DATA_LOADING:
trace("Loading Site Date...");
break;
case ApplicationFacade.SITE_DATA_LOADED:
trace("Site Data Loaded");
break;
case LoadupMonitorProxy.CALL_OUT_OF_SYNC_IGNORED:
trace("Abnormal State, Abort");
break;
case LoadupMonitorProxy.LOADING_PROGRESS:
var percent : Number = note.getBody() as Number;
trace("Loading Progress: " + percent + "%");
break;
case LoadupMonitorProxy.LOADING_COMPLETE:
trace(">>Loading Complete");
initializeView();
break;
case LoadupMonitorProxy.LOADING_FINISHED_INCOMPLETE:
trace("Loading Finished Incomplete");
break;
}
}

private function initializeView() : void {
sendNotification(ApplicationFacade.PREPARE_GALLERY);
}

public function get stage():Stage{
return viewComponent as Stage;
}

My Problem is now, that my GalleryMediator and my StageMediator listen to the LoadupMonitorProxy.LOADING_COMPLETE or Loadup.LOADING_COMPLETE notification.

Now the initializeView() functions of the StageMediator get called twice. How i can fix the Problem?
Logged
puremvc
Global Moderator
Hero Member
*****
Posts: 2871



View Profile WWW Email
« Reply #7 on: December 10, 2009, 09:43:41 »

I don't see the GalleryMediator getting registered here. But it seems like it shouldn't be listening for LoadupMonitorProxy.LOADING_COMPLETE, but instead ApplicationFacade.PREPARE_GALLERY.

-=Cliff>
Logged
pirol
Newbie
*
Posts: 7


View Profile Email
« Reply #8 on: December 10, 2009, 12:03:16 »

ApplicationFacade.PREPARE_GALLERY calls a command

:
override public function execute(note : INotification) : void {
var stageMediator : StageMediator = facade.retrieveMediator(StageMediator.NAME) as StageMediator;
var siteDataProxy : SiteDataProxy = facade.retrieveProxy(SiteDataProxy.NAME) as SiteDataProxy;
var gallery : Gallery = new Gallery();

for (var i : int = 0; i < siteDataProxy.images.length; i++) {
gallery.addImage(new Image(String(i), siteDataProxy.urls[i]));
}

stageMediator.stage.addChild(gallery);
facade.registerMediator(new GalleryMediator(gallery));
}
Logged
puremvc
Global Moderator
Hero Member
*****
Posts: 2871



View Profile WWW Email
« Reply #9 on: December 10, 2009, 06:55:26 »

Ok, so the GalleryMediator isn't registered until after the StageMediator hears LoadupMonitorProxy.LOADING_COMPLETE. So why do you have GalleryMediator being interested in LoadupMonitorProxy.LOADING_COMPLETE?

-=Cliff>
Logged
pirol
Newbie
*
Posts: 7


View Profile Email
« Reply #10 on: December 11, 2009, 08:48:19 »

Hi, the GalleryMediator is interested in Loadup.LOADING_COMPLETE which i think is the same. It shows me when all images are loaded.

My problem is, that the Startup and the loading of the images listen to the same Proxy (Loadup Utility) so all mediators handle when the Loaduputility sends the LoadingComplete notification.

Logged
puremvc
Global Moderator
Hero Member
*****
Posts: 2871



View Profile WWW Email
« Reply #11 on: December 11, 2009, 12:22:03 »

If the GalleryMediator is registered by the StageMediator when the StageMediator recieves LOADING_COMPLETE, then the GalleryMediator doesn't need to be interested. By the order of things, it has already happened and should simply assume that loading is complete when it's registered. In onRegister, it should be able to retrieve the data it needs and display it.

Personally, I would suggest using the StateMachine utility to define discrete states of your app and have the mediators follow state changes.

-=Cliff>
Logged
pirol
Newbie
*
Posts: 7


View Profile Email
« Reply #12 on: December 12, 2009, 04:50:01 »

Hi, my problem isn't the GalleryMediator. It have not to listen to the LOADING_COMPLETE but the LoadupUtility dispatchs the Notification twice. First, when the data is loaded and second, when the images are loaded.
My StageMediator listen to this LOADING_COMPLETE and execute twice the function "initializeView"...Therfore the View is gerneratet twice...

Can I say to the StageMediator that it just have to listen for the notifactions until it receive the LOADING_COMPLETE notification?
Logged
puremvc
Global Moderator
Hero Member
*****
Posts: 2871



View Profile WWW Email
« Reply #13 on: December 13, 2009, 08:35:43 »

Ok. Looks like you need to plan your overall sequence of events little better. Personally, I compartmentalize all the activities of the application such as loading into discrete states using the StateMachine Utility, and I have major things like displaying the view follow the state changes.

I'm not the authority on working with the Loadup utility, though. For questions specific to working with the Loadup Utility, please post to the thread for the utility itself:

http://forums.puremvc.org/index.php?topic=1397.0

-=Cliff>
Logged
philipSe
Sr. Member
****
Posts: 139


View Profile Email
« Reply #14 on: December 14, 2009, 04:19:38 »

It have not to listen to the LOADING_COMPLETE but the LoadupUtility dispatchs the Notification twice. First, when the data is loaded and second, when the images are loaded.
If there is a question about this Loadup behaviour, I have a comment that relates to notification type.  But best to keep that stuff to the Loadup thread as Cliff says.
----Philip
Logged
Pages: [1]
Print