This post builds on my last post regarding the use of the ViewMediator pattern with Swiz. One of the things I hadn’t tested when I wrote the original AbstractViewMediator (AVM) was popups. Once I started using this base class in a couple projects I realized I had a pretty big hole that I quickly need to fix.
Download the new and improved AbstractViewMediator.
Normally, views are added to the display list, but popups are actually added to the SystemManager so they don’t fire off the flash.events.Event.ADDED_TO_STAGE event that’s mediated in the setView() method in my AbstractViewMediator and that’s the core piece of logic behind the entire AbtractViewMediator. See the last post for details.
So how would I connect my views to my VMs? The key was to make the AbstractViewMediator actually create the popup via the following method:
public function createPopupView(
view:IFlexDisplayObject,
parent:DisplayObject,
modal:Boolean = false,
childList:String = null,
moduleFactory:IFlexModuleFactory = null
):IFlexDisplayObject
{
this.setView(view);
PopUpManager.addPopUp(view, parent, true); // window:IFlexDisplayObject,parent:DisplayObject,modal:Boolean = false,childList:String = null,moduleFactory:IFlexModuleFactory = null
PopUpManager.centerPopUp(view);
return view;
}
If you look at the method signature you’ll see that I pass in a reference to the view we’re creating and then call the all important public function setView(value:*):void method that’s usually called by the mediated event flash.events.Event.ADDED_TO_STAGE. This is what creates the connection between a VM and a View. This also brings up a new question — who calls this method?
In order for the VM to be hooked up to the correct view, we’ll need to make sure we call the createPopupView(...) method on our concrete VM implementation…hmm…so how do we get a hold of that…ahh yes, the nice guys at Swiz gave us access to it via the list of beans that this instance of Swiz has. Let’s learn by example.
We’ll imagine we need to open a popup from MainView after clicking the “Get Users Button”. Naturally, our MainViewMediator mediates the button’s click event and calls this functional method:
/**
* Open the create user view.
*/
public function openCreateUserView():void
{
var view:CreateUserView;
var vm:CreateUserViewMediator;
view = new CreateUserView();
vm = this.getViewMediatorBeanById("createUserViewMediator") as CreateUserViewMediator;
vm.createPopupView(view, this.view, true);
}
The key line is where we get a handle to the CreateUserViewMediator via the method getViewMediatorBeanById(...) in the AbstractViewMediator:
/**
* Helper method that returns a view mediator managed by Swiz by it's ID.
*
* @param id The unique ID for the view mediator as defined in the BeanLoader.
*/
public function getViewMediatorBeanById(id:String):AbstractViewMediator
{
var vm:AbstractViewMediator = this.swiz.beanFactory.getBeanByName(id).source as AbstractViewMediator;
return vm;
}
/**
* Helper method that returns a view mediator managed by Swiz by it's ID.
*
* @param clazz The class type for our view mediator.
*/
public function getViewMediatorBeanByType(clazz:Class):AbstractViewMediator
{
var vm:AbstractViewMediator = this.swiz.beanFactory.getBeanByType(clazz).source as AbstractViewMediator;
return vm;
}
This method in the AVM also assumes it has a handle to the instance of Swiz that contains the beans. This is done by implementing the ISwizAware interface in the AVM.
/**
* Creates a reference to the instance of Swiz this bean is in. This
* allows us to get a hold of manager beans by using:
* <code>var vm:MyViewMediator = swiz.beanFactory.getBeanByName("myViewMediator").source as MyViewMediator;</code>
*/
public function set swiz(swiz:ISwiz):void
{
this._swiz = swiz;
}
public function get swiz():ISwiz
{
return this._swiz;
}
protected var _swiz:ISwiz;
There you have it. All the pieces. If you create popups this way you’ll get the expected VM to View connection.
#1 by Prashant - September 1st, 2010 at 12:12
Thanks for these great blogs…helped me get started on Swiz.
But I do have trouble running this one:
1. I downloaded your app from: http://www.webappsolution.com/wordpress/2010/01/07/swiz-passive-view-example-part-2/ this works.
2. Replaced LoginViewMediator.as with http://www.webappsolution.com/wordpress/2010/04/20/swiz-10-rc1-removes-autowire-viewtrue-requires-new-abstractviewmediator/
3. Replaced AbstractViewMediator.as with the one on this blog
4. Replaced \libs\swiz-0.6.4-flex3.swc with swiz-framework-1.0.0-RC1.swc from http://swizframework.jira.com/wiki/download/attachments/1998854/swiz-framework-1.0.0-RC1.zip
On compilation code errors out:
- Cannot resolve attribute ‘beanLoaders’ for component type org.swizframework.core.SwizConfig.
- Cannot resolve attribute
‘mediateBubbledEvents’ for component type org.swizframework.core.SwizConfig.
code it compains on:
EmpMgmtConsoleSwizPassiveView
Could you please guide? Thanks.
#2 by Prashant - September 1st, 2010 at 12:27
Thanks for these great blogs…helped me get started on Swiz.
But I do have trouble running this one:
1. I downloaded your app from: http://www.webappsolution.com/wordpress/2010/01/07/swiz-passive-view-example-part-2/ this works.
2. Replaced LoginViewMediator.as with http://www.webappsolution.com/wordpress/2010/04/20/swiz-10-rc1-removes-autowire-viewtrue-requires-new-abstractviewmediator/
3. Replaced AbstractViewMediator.as with the one on this blog
4. Replaced \libs\swiz-0.6.4-flex3.swc with swiz-framework-1.0.0-RC1.swc from http://swizframework.jira.com/wiki/download/attachments/1998854/swiz-framework-1.0.0-RC1.zip
compilation errors:
- Cannot resolve attribute ‘beanLoaders’ for component type org.swizframework.core.SwizConfig.
- Cannot resolve attribute
‘mediateBubbledEvents’ for component type org.swizframework.core.SwizConfig.
code it compains on:
EmpMgmtConsoleSwizPassiveView
Could you please guide? Thanks.
#3 by Prashant - September 1st, 2010 at 12:28
#4 by Jash - October 1st, 2010 at 09:46
Hi
Could you provide an example with the latest AbstractViewMediator in actions along with swiz.1.0 RC1 or RC2.
We find it very tough to get the application running with the old source files and the new AbstractViewMediator.as
This wud emmensely help us use the Passive view approach in a useful manner.
Thanks
#5 by Jash - October 4th, 2010 at 05:03
Hi
There is one peculiar problem found when using the AbstractViewMediator. Usually in the clean up functions of the VM’s we add the removeEventListeners for the mouse events etc. suppose when we resize our app by clicking on browser , the clean up method gets called for all VM’s and the event listeners are removed. After this the user is not able to click anywhere and do something..
any idea how to overcome this.