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: What's the best practice for Flash Vars?  (Read 8648 times)
jpwrunyan
Sr. Member
****
Posts: 84


View Profile WWW Email
« on: February 25, 2010, 11:13:40 »

Ok, so I am currently trying to clean up the spaghetti that is our current ApplicationFacade and its immediate mediators.  Our application is dependent upon settings that are located in the flashVars of the encapsulating HTML object tag (we use swfobject if you're curious).  From those vars we determine what database the application should then retrieve its information from (these settings are downloaded from a url in flashVars).

Currently, flash vars are grabbed inside the facade class and then a url request is launched via URLLoader then and there.  When the request comes back, more fun stuff is done where the facade--which kept a global reference to the Application instance it received in the startUp() command--passes the URLoader's result to the application view component (yeah! who needs a Mediator, right?).

Well, all this works, but I feel like to is definitely not correct (correct me if I am wrong).
The place where I am particularly facing a moral conundrum is the flashvars itself.

Here's part one of my question:
I feel as though the flash vars should have its own proxy (registered in the new StartUpCommand) but flashvars comes directly from the view component via: view.root.loaderInfo.parameters.

I could just pass that object in the StartUpCommand:

:
facade.registerProxy(new FlashVarsProxy("FlashVarsProxy", body.root.loaderInfo.parameters)) //body is an instance of Application
But since its bound directly to the application instance, should it be handled inside the Mediator as a sort of public var?

And now for part two of my question:

Once I decide what to do with the flash vars, who/what should be responsible for the URLLoader request for remote variables based on those flash vars?

Again, I am leaning towards just declaring a proxy for that in the StartUpCommand and setting a search parameter based on the flash vars.  Perhaps via the FlashVarsProxy I register on the line just before it (or not)?

:
var systemConfigProxy = SystemConfigProxy(facade.registerProxy(new SystemConfigProxy()));
systemConfigProxy.dataURL = body.root.loaderInfo.parameters.serverURL; //and now wait for notification when the URLLoader is complete

Any thoughts much appreciated, no matter how detailed or specific!
Logged
jpwrunyan
Sr. Member
****
Posts: 84


View Profile WWW Email
« Reply #1 on: February 26, 2010, 12:03:44 »

Ok, well, here's my proposed solution to the above (irrelevant code elided):
:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical"
applicationComplete="applicationCompleteHandler(event)">

<mx:Script>
<![CDATA[
import mx.events.FlexEvent;
import mx.core.Application;

private function applicationCompleteHandler(event:FlexEvent):void {
var facade:ApplicationFacade = new ApplicationFacade();
facade.startApplication(this);
}
]]>
</mx:Script>
</mx:Application>
:
public class ApplicationFacade extends Facade {

public static const START_APPLICATION:String = "startApplication";

override protected function initializeController():void {
super.initializeController();
registerCommand(START_APPLICATION, AppStartUpCommand);
}

public function startApplication(applicationView:Application):void {
sendNotification(START_APPLICATION, applicationView);
removeCommand(START_APPLICATION); //why not?
}
}
:
public class AppStartUpCommand extends SimpleCommand {

override public function execute(notification:INotification):void {
var app:Application = notification.getBody() as Application;

var appMediator:ApplicationMediator = new ApplicationMediator(app);
facade.registerMediator(appMediator);

facade.registerProxy(new FlashVarsProxy());
facade.registerProxy(new SystemConfigProxy());

//business logic will resume with SystemConfigProxy.NEW_DATA notification
}
}
:
public class FlashVarsProxy extends Proxy {

public static const NAME:String = "FlashVarsProxy";

public function FlashVarsProxy(proxyName:String=null, data:Object=null) {
if (!proxyName) {
proxyName = NAME;
}
if (!data) {
data = Application.application.root.loaderInfo.parameters;
}
super(proxyName, data);
}
}
systemURL is just a url to download from (I made it a package variable)
:
public class SystemConfigProxy extends Proxy {

public static const NAME:String = "SystemConfigProxy";
public static const NEW_DATA:String = "SystemConfigProxy.newData";

public function SystemConfigProxy(proxyName:String=null, data:Object=null) {
if (!proxyName) {
proxyName = NAME;
}
super(proxyName, data);
}

override public function setData(data:Object):void {
super.setData(data);
//initial business logic and any concomitant mediators
//will respond to this notification appropriately
sendNotification(NEW_DATA, data);
}

override public function onRegister():void {
var flashVars:Object = facade.retrieveProxy(FlashVarsProxy.NAME).getData();
var systemId:String = flashVars.systemId;

var request:URLRequest = new URLRequest(systemURL + systemId + ".xml");
var loader:URLLoader = new URLLoader(request);
loader.addEventListener(Event.COMPLETE, loaderHandler);
}

private function loaderHandler(event:Event):void {
var loader:URLLoader = URLLoader(event.target);
loader.removeEventListener(Event.COMPLETE, loaderHandler);

setData(loader.data);
}
}
Well, that's about the best I can come up with (until Monday anyway).
« Last Edit: February 26, 2010, 02:01:32 by jpwrunyan » Logged
puremvc
Global Moderator
Hero Member
*****
Posts: 2871



View Profile WWW Email
« Reply #2 on: February 26, 2010, 10:56:29 »

Here's a link to a description of the way I handle it. Regarding parameters passed to the application, I don't worry about where the data is coming from, it's data. A ParameterProxy is easily done:

http://forums.puremvc.org/index.php?topic=622.msg2781#msg2781

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


View Profile WWW Email
« Reply #3 on: February 28, 2010, 06:42:03 »

awesome.
thanks for the link.
that's actually how I ended up doing it after I posted the code above.

and as always, thanks for the feedback!  Since I'm re-writing our framework level classes, everything I rewrite gets challenged (as it should) so it's nice to have an expert opinion to fall back on when justifying my changes!

The problem I have with my original source above is that the flash vars proxy assumes it has access to the Application singleton.  That works inside of Flex, but it would not work for any other framework.  Therefore it is better to grab the parameters from the actual instance as you did and pass them to the FlashVarsProxy constructor.  The fact that that instance is the Application view component is irrelevant.

Anyway, just thinking out loud here...
Logged
Pages: [1]
Print