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: When to initialize: in preloader or in main?  (Read 10884 times)
sventunus


Email
« on: February 28, 2011, 02:40:50 »

Dear community,

I have a question concerning a pure AS3 flash project with a preloader in frame 1.

I am wondering where and when I should initialize the PureMVC apparatus: in the preloader or in the main class that gets constructed once the preloader finishes it's job?
My gut feeling would say to start everything in the main class, registering the main view with the Facade through my applicationmediator and going on from there.

However, all the stuff that the preloader loads before launching the website has to remain available to the application. So maybe it would be better to call startup in the preloading stage (registering a mediator for the preloader and a proxy to do the actual preloading) and subsequently execute an initialize-macrocommand to initialize the whole website once the preloader has finished, or throw an error and destruct everything if preloading should fail.

What would be your recommendations?

Thanks in advance!
Logged
sventunus


Email
« Reply #1 on: February 28, 2011, 10:05:34 »

Thought I might answer my own question here.

I initialized everything in the preloader, and this looks perfectly ok to me. Please let me know if you have any suggestions or remarks.
Hope this may help someone else who's in the same situation  :)

Preloader.as (irrelevant code omitted)
:
package com.thesedays.web2011
{
// import statements

/**
* @author Sven Dens - These Days
*/
public class Preloader extends MovieClip
{
// events constants
public static const STORE_CONFIGURATION:String = "event/store/configuration";
public static const BACKGROUND_SET:String = "event/background/set";

// Get a facade instance for this application
    private var _facade:ApplicationFacade = ApplicationFacade.getInstance( Web2011.NAME );
   
    // main application class
private var _main:Class;

// application class instance
private var _app:Object;

// stage assets for preloading
private var _loadingAnimation:TDLoader;
private var _background:Bitmap;

// preloader byte counters
private var _swfPercentLoaded:Number;
private var _backgroundPercentLoaded:Number;
private var _dataPercentLoaded:Number;
private var _totalProgress:Number;
   
    // the configuration that was received from the flashvars (or default config)
    private var _config:ConfigurationVO;
   
    // greensock Imageloader for the background image used in the preloader
    private var _backgroundLoader:ImageLoader;
   
   
    //-------------------------------------
    // CONSTRUCTOR
    //-------------------------------------
    public function Preloader()
    {
    // stop the timeline
stop();

// initialize the byte counters
_swfPercentLoaded = 0;
_backgroundPercentLoaded = 0;
_dataPercentLoaded = 0;
_totalProgress = 0;

// start the PureMVC apparatus
    _facade.startup(this);

// set scale mode
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;

// activate plugins
TweenPlugin.activate([AutoAlphaPlugin]);

// load policies and set permissions for YouTube
Security.loadPolicyFile('http://www.youtube.com/crossdomain.xml');
Security.allowDomain("*.youtube.com");
Security.allowDomain("*.ytimg.com");

// load the fonts
loadFonts();

// initialize application settings
readFlashVars();

// listen for enter frame
addEventListener(Event.ENTER_FRAME, onEnterFrame, false, 0, true);

// listen for resize
stage.addEventListener(Event.RESIZE, onResize, false, 0, true);
    }

private function onLoadingAnimationFinished(evt:Event) : void
{
// remove event listener
_loadingAnimation.removeEventListener(TDLoader.ANIMATION_FINISHED, onLoadingAnimationFinished);

// advance to frame 2
nextFrame();

// fade out the preloader and render the website
renderWebsite();
}

private function renderWebsite():void
{
_main = Class(getDefinitionByName("com.thesedays.web2011.Web2011"));

if(!_main) throw new Error("Could not retrieve the main application class com.thesedays.web2011.Web2011. Check your code!");

// instantiate the website
_app = new _main();
addChild(_app as DisplayObject);

// and call the startup method
_app.startup();

// now clean up the preloader stuff
var i:int = numChildren;
while(--i > -1)
{
if(getChildAt(i) == _loadingAnimation)
{
removeChildAt(i);
_loadingAnimation = null;
break;
}
}

i = NaN;
_swfPercentLoaded = NaN;
_backgroundPercentLoaded = NaN;
_dataPercentLoaded = NaN;
_totalProgress = NaN;
    _config = null;
}


StartupCommand
:
package com.thesedays.web2011.controller
{
//import statements

/**
* @author Sven Dens - These Days
*/
public class StartupCommand extends SimpleCommand
{
override public function execute ( note:INotification ) : void
{
var preloader:Preloader = note.getBody() as Preloader;
if(!preloader) throw new ArgumentError("The object passed to the startup method could not be cast as a Preloader, check your code!");

// register a mediator for the Preloader
facade.registerMediator(new PreloaderMediator(preloader));

// register a proxy to store the configuration that was provided with flashvars
facade.registerProxy(new ConfigurationProxy());

// register a proxy to store the application's data that will be preloaded
facade.registerProxy(new ApplicationDataProxy());

// and register a proxy to store the backgrounds that will be loaded from the server
facade.registerProxy(new BackgroundsProxy());

}
}
}

And then the main class, Web2011.as
:
package com.thesedays.web2011
{
import flash.display.MovieClip;

/**
* @author Sven Dens - These Days
*/
[Frame(factoryClass="com.thesedays.web2011.Preloader")]
public class Web2011 extends MovieClip
{
// App name
    public static const NAME:String = "Web2011";
   
public function Web2011()
{

}

public function startup():void
{
trace("application started");
}
}
}
Logged
puremvc
Global Moderator
Hero Member
*****
Posts: 2871



View Profile WWW Email
« Reply #2 on: March 10, 2011, 09:56:06 »

Normally I build Flex or AIR apps, so initializing PureMVC in the preloader doesn't make sense, because the initial display hierarchy hasn't been built yet. However in the view preparation phase of PureMVC startup I usually want to wrap mediators around existing components in the display list.

Also, view preparation should come after model preparation, since the mediators generally want to retrieve references to the proxies they collaborate with in their onRegister handlers.

I think this process still makes sense in an AS3-only application, it's just that your view preparation phase will include creating the display list as well as mediating it.

What do you gain by doing this in the preloader? I can tell you what you lose: the preloader can't be reused another application now, because it has a dependency on the ApplicationFacade for this one application.

-=Cliff>
Logged
puremvc
Global Moderator
Hero Member
*****
Posts: 2871



View Profile WWW Email
« Reply #3 on: March 10, 2011, 10:17:04 »

Another question I'd ask is why the preloader needs to do loading of all these assets?

The role typically served by the preloader is to give you a way to have something happening visually while the rest of the SWF loads. After that, your app should take over. By putting all this asset management into the preloader, you're really overloading it (no pun intended) with responsibilities better left to the application during an ordinary PureMVC startup process.

The simpler the preloader, the more reusable it is. Here are some examples:

http://darkstar.puremvc.org
http://puremvc.tv
http://contact.futurescale.com

-=Cliff>
« Last Edit: March 10, 2011, 10:18:44 by puremvc » Logged
Pages: [1]
Print