Posts Tagged Proxy

How to handle Stage resizes with PureMVC

I am starting to like the Proxies a lot in PureMVC because I am always creating new ones (Google Analytics Proxy, SWFAddress Proxy, etc) and they are reusable from project to project.

The latest one is a Proxy that keeps track of the resizing of the stage. I found it was a nice way to handle the problem where you set the stage to NOSCALE and you want to handle the resizing yourself. In those situation you would have multiple views/elements reacting differently + you would have to manage adding and removing listener to the stage. Now with one Proxy setting one listener to the stage and sending notifications whenever it resizes, I found it was simpler. Here is the code for my Proxy:

package com.zedia.model{
  import com.zedia.ApplicationFacade;
  import flash.display.Stage;
  import flash.events.Event;
  import flash.events.MouseEvent;
  import org.puremvc.as3.interfaces.IProxy;
  import org.puremvc.as3.patterns.proxy.Proxy;
/**
* Model for the everything that is related to Resizing.
*
*/
  public class ResizeProxy extends Proxy implements IProxy{
    public static const NAME:String = "ResizeProxy";
    private var _stage:Stage;
    public function ResizeProxy(newStage:Stage){
      super(NAME);
      _stage = newStage;
      _stage.addEventListener(Event.RESIZE, _onStageResize, false, 0, true);
    }
 
    public function get stageWidth():Number {
      return _stage.stageWidth;
    }
 
    public function get stageHeight():Number {
      return _stage.stageHeight;
    }
 
    private function _onStageResize(event:Event):void {
      facade.sendNotification(ApplicationFacade.STAGE_RESIZE);
    }
  }
}

Now in the StartupCommand of your application you register the Proxy and pass it an instance of the stage. It is easy to do so because you already have to pass one instance of the stage to the ApplicationMediator. Once that is done, your Proxy is running and sending the notifications. All that is left is to listen and to handle the notifications in the Mediators of the views that needs to react to the resizing; as simple as that.

, ,

10 Comments