Configuring your ARP application

ARP, Flash Add comments

There was a recent post on the ARP mailinglist about how an application could be configured. Since there is (still) not a uniform way of doing so in ARP I thought I'd share the solution I came up with. The whole idea is based on an XML file that contains key/value pairs, pretty much like the appsettings implementation in .NET. The XML file, called "appsettings.xml", gets loaded in the ArpApplication class. This is a new class I introduced to the framework that all Application forms extend. The Application form being the applications point of entry.

Before diving into any code examples, please note that these are my personal additions to the framework and that they are not in any official release. Other additions from the ARP Advisory Committee members are available as well: Peter Hall, Jesse Warden, Shannon Jackson and the ARP labs overview.

appsettings.xml

Here's an example of the appsettings.xml file. As you can see all entries consist of a key/value pair. In order for your application to find the appsettings file, you should either:
- name it "appsettings.xml" and put it in a folder where your swf can read it without changing folders.
- use flashvars in the html file that embeds the swf and create a var called "appSettingsPath". Point its value to the appsettings file in any directory. Note that the name "appsettings.xml" can be changed then and that it can even be a serverside page that generates a dynamic config.

XML:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <appsettings>
  3.     <add key="applicationBackground" value="0x999999"/>
  4.     <add key="menuItems" value="Home,News,Projects,Contact"/>
  5.     <add key="menuBackground" value="0xCCCCCC"/>
  6.     <add key="content" value="Lorem Ipsum is simply ..."/>
  7. </appsettings>

To access one of the values, use the AppSettings class.

Actionscript:
  1. var menuItems:String = AppSettings.getInstance().getValue("menuItems");
  2. var menuBackground:Number = Number(AppSettings.getInstance().getValue("menuBackground"));

Note that these values can only be fetched when the appsettings file has been loaded and parsed. Therefore your Application class needs to extend the ArpApplication class. To know when the loading and parsing is done, you need to implement the onAppSettingsLoad() method. This is a (abstract) method in ArpApplication that gets called when the values have been parsed.

In the following code we set the backgroundcolor of the application to a value in the appsettings and then call the show() method on 2 subforms: menuForm and contentForm.

Actionscript:
  1. /**  
  2. * Overwritten from ArpApplication.
  3. */
  4. function onAppSettingsLoad(){
  5.   trace(toString() + " - onAppSettingsLoad");
  6.  
  7.   var applicationBackgroundColor:Color = new Color(background_mc);
  8.   applicationBackgroundColor.setRGB(Number(AppSettings.getInstance().getValue("applicationBackground")));
  9.            
  10.   menuForm.show();
  11.   contentForm.show();   
  12. }

I overwrote the show() method in these subclasses because they also need values from the appsettings file. And now that I think about it, doing so isn't the best idea. I can see how pretty intense operations are called everytime we hide/show the forms. Either letting the application form dispatch events when the settings are loaded could be a better solution or if there was any way of intercepting the onLoad() handler of the subforms, that could be a good solution too. That way, we could trigger the onLoad() handler on the subforms only when the appsettings are loaded. (In fact, I have been trying to catch the onLoad() before it fires without success. So if anyone knows, please let me know.)

Here is a zip file with a small example and the ArpApplication and the AppSettings class.
Put the AppSettings class in the package org.osflash.arp.config.* and the ArpApplication class in org.osflash.arp.view.*


Add to Bloglines - Digg This! - del.icio.us - Stumble It! - Twit This! - Technorati links - Share on Facebook - Feedburner
 

2 Responses to “Configuring your ARP application”

  1. Aral Says:

    Hi Christophe,

    Nice approach. I also like having a core Application form class — has a nice parallel with the Flex way of doing things. As we bring this in to the framework for the next release, I would suggest that we use org.osflash.arp.flash.Application and org.osflash.arp.flex.Application — what do you think? (I added flash and flex as in flex, we will need to extend mx.core.Application whereas in Flash we can extend ArpForm.)

  2. Arie Says:

    Christophe,

    I wonder if you are still using this approach. IMHO there could be a probleem in what comes first in the application: the onLoad or the onAppSettingsLoad call.
    It’s probable safer to fetch the appsettings after we receive the onLoad, so we can be shure of the order of the methodcalls.

    Arie

Leave a Reply

WP Theme & Icons by N.Design Studio
Entries RSS Comments RSS Login