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.

, ,

  1. #1 by Will - March 25th, 2009 at 21:22

    I would consider the stage as residing squarely in the View tier of the MVC triad. It is always going to be the root object of all other views.

    Here you are listening for user input, which by it’s nature is a property of View components. Therefore it should not be talking directly to a Proxy, it should be communicating with a Mediator which passes on the message. What you have isn’t going to break anything but I’m not sure if it’s considered a best practice.

  2. #2 by zedia.net - March 25th, 2009 at 23:21

    I get what you are saying, but this thing acts the same way as a Model. You need to be able to get the size of the stage from any part in your application, and not just when it is resized.

    Ex: if the stage was resized before you create a View, that View still need to be able to get the size of the stage, to adjust to it even if it will never be resized again .

    If it wasn’t for that I would agree with you, you could send the stageHeight and stageWidth in the notification and everything would be great. But I need more than this.

  3. #3 by Fardeen - April 8th, 2009 at 09:04

    I would have done it in a meditator too. (even if i think it would be useless)

  4. #4 by landed - July 31st, 2009 at 12:14

    How could I get a ref to the stage and gets say its width using the facade for example then ?

  5. #5 by zedia.net - July 31st, 2009 at 13:04

    @ landed
    In any mediator you can do this:

    var resizeData:ResizeProxy = facade.retrieveProxy(ResizeProxy.NAME) as ResizeProxy;

    //this will trace you the current stage width
    trace (resizeData.stageWidth)

  6. #6 by jimb - October 22nd, 2009 at 15:08

    I’ve seen implementations using both proxy and mediators to handle the stage.
    Best practice is to use a Mediator however.
    http://forums.puremvc.org/index.php?topic=1121.msg5164#msg5164

  7. #7 by jfox015 - December 9th, 2009 at 13:54

    I have been thinking of putting together a SWFAddress proxy for my AS3 PureMVC framework FoxR. Would you be interested in sharing notes on your take of it?

  8. #8 by zedia.net - December 9th, 2009 at 14:40

    I normally would, but I am swamped right now… maybe later

  9. #9 by jfox015 - December 9th, 2009 at 15:53

    Word. You ever free up, give me a shout.

    -JF

  10. #10 by Russ - March 17th, 2010 at 15:17

    I added an object that holds the current stage width, height to the notification the proxy sends so I wouldn’t have to reference it in my mediator unless I needed to get the current measurement.
    facade.sendNotification(ApplicationFacade.STAGE_RESIZE,{swidth:_stage.stageWidth,sheight:_stage.stageHeight});

(will not be published)
Subscribe to comments feed
  1. No trackbacks yet.