2

I'm making an app with flash AS3 and puremvc, the way to correctly handle view mediators is sort of throwing me a little...

OK - I have 3 or 4 distinct views, each governed by it's own mediator. Each view is only displayed on its own - ie. when 1 is visible/on stage - the others are invisible/removed from stage (over simplified, but I guess could be thought of as seperate pages, viewed one at a time, a nav bar allows a user to change views when they like)

At first each view mediator I had creating and added to stage it's own view component, and as such, when it was 'turn' to show or hide that view, it was simple - addChild and removeChild for it's view component.

However, I read over at puremvc.org that it's not good practice to a. pass round the stage (which was the viewComponent for each mediator in my case - where each view was added to - eg. viewComponent.addChild(foo) b. create its own view - which allowed me to add and remove it in the first place.

So I changed to the recommended way - when I create each mediator, what I pass in is the view component it governs (rather than the stage/main doc class)

eg.

var view:MyView = new MyView();
facade.registerMediator( new MyViewMediator( view ) );
viewComponent.addChild(view);

So - with that the case - I have no idea how I would go about removing/adding each view when needed. I could easily enough set the visibility from within each mediator, but I sort of wanted to avoid doing that - prefer removing when not in use for resource management...

Anyone got any good ideas either how I am 'meant' to go about this (as in, when a view is no longer needed on stage, remove it - temporarily, and add it later on when it is needed?) - or am I missing some point (quite likely!) and going about this the wrong way? I'm very new to puremvc so not too confident I am approaching it right. Pointers most welcome!

Calvin
  • 21
  • 2

3 Answers3

1

I won't pretend to be an expert in PureMVC, but the way I like to work is to only create a Mediator for major areas of the site. For example, a HeaderMediator, FooterMediator and ViewMediator. Then, I have an ApplicationMediator that "onRegister" will create the other mediators just mentioned. This way, I'm not creating a mediator for each individual view and the "ViewMediator" handles switching between views. Here is what my "onRegister" function would look like:

override public function onRegister():void {
    var viewContainer:Sprite = viewComponent.addChild(new Sprite()) as Sprite;
    facade.registerMediator(new ViewMediator(viewContainer));

    var headerContainer:Sprite = viewComponent.addChild(new Sprite()) as Sprite;
    facade.registerMediator(new HeaderMediator(headerContainer));

    var footerContainer:Sprite = viewComponent.addChild(new Sprite()) as Sprite;
    facade.registerMediator(new FooterMediator(footerContainer));
}

Next, I'll have a ViewProxy that has a pool of views sitting and waiting to be called up (in an Array, Dictionary or Vector). When it's time to change or set a view, I use a ChangeViewCommand that retrieves the view from the ViewProxy and sends a notification (like SET_VIEW) with the view object as the body.

Now, the ViewMediator will handle the notification "SET_VIEW". If it's something as simple as removeChild(oldView) and addChild(newView) then I will just handle it in the Mediator itself. If a transition is necessary and involves more code, then I'll use a command to handle the transition.

Hope that makes sense and helps. Of course, I like to change things up and try new things, so if you have any concerns or suggestions I'm open to hearing them.

Corey
  • 5,818
  • 2
  • 24
  • 37
  • Thanks for the reply. Sounds like you are doing roughly the same as me - creating the "other" mediators in ApplicationMediator. So given the set up you describe - the 'I will just handle it in the Mediator itself' bit - how are you doing that? eg. your ViewMediator - you are passing the view (viewContainer) into the mediator when you create it, so inside ViewMediator, how exactly do you remove the viewContainer? – Calvin Aug 18 '11 at 20:46
  • eg. first time you want it removing (assuming it's on display list) - viewComponent.parent.removeChild(viewComponent); would do it - but how to you add it back on? You can't go viewComponent.parent.addChild(viewComponent); as viewComponent no longer has a parent... See what I mean? Or am I missing something? – Calvin Aug 18 '11 at 20:46
0

May I ask your motivation for using PureMVC ? I don't want to start a holy war here, but one of the reasons behind the creation of the second generation frameworks (Mate,Swiz,Robotlegs,Parsely) was because of the absurd amount of difficulty in getting started with RIA application architecture.

Don't feel bad though, the newer frameworks have documentation, so it is easy to understand and figure out the motivation for all of the components, paradigms and actors that come into play.

Black Dynamite
  • 4,067
  • 5
  • 40
  • 75
  • It's OK - I don't feel bad :-) I actually quite like PureMVC straight away - for AS3 work. For Flex I'm not so sure - but again, I've only just started looking at it. – Calvin Aug 16 '11 at 21:46
  • Oh and why I'm using it for AS3 - just figured it was about time I didn't do everything 'custom' and made stuff to an accepted framework/meta-patterns/whatever you want to call it. PureMVC is widely used and so worth some attention (and again, I do quite like it - and yes, it has drawbacks, but so does everything) Once I get to grips with this, I'll probably look at a Robotlegs version. Til then, still quite like to know the answer to this question! – Calvin Aug 16 '11 at 21:49
0

As FlexFiend mentioned, this situation is easier to take care of in second generation frameworks (at least it is in Robotlegs, I can't speak for the others). This is because, usually, you never explicitly create or destroy a mediator in Robotlegs; instead, you associate a mediator with a view component and Robotlegs constantly watches the stage for view components being added and removed and creates and destroys instances of the appropriate mediators.

Therefore one approach for implementing this kind of thing in PureMVC is to have a StageMediator with the stage itself as its view component, and listen for Event.ADDED_TO_STAGE and Event.REMOVED_FROM_STAGE events. Finally you will need a system to associate view components with mediators. Check out https://github.com/robotlegs/robotlegs-framework/blob/master/src/org/robotlegs/base/MediatorMap.as to see how its done in Robotlegs.

I am actually about to implement something like this since I am working on a project which is part of a suite of PureMVC applications, but honestly unless you have no choice I would say the easiest thing to do is simply to use Robotlegs!

devdave
  • 688
  • 5
  • 6