Flex user defined runtime settings Part 2

Tags: AS3,Flex
Word Count: 166

In the 1st part of this series we looked at the thought process of using dynamic settings in Flex. In part 2 I want to focus on the implementation of reading in an XML file. While you can find many articles out there about reading XML files this article will focus on the problems of web minded thinking. When I first started this project I that prompted this post  I thought, reading a settings file and using the values, that's easy enough. Before we look at a solution we need to take a quick look at the wrong solution. Coming from a web background my mind is very top down procedure driven so I figured this would work. I would create a creation complete event, read in the settings file and use the values. In this example I want to read my settings file and use the panel title setting in my application.

Looks pretty standard right, in fact if you compiled this you would not get any errors. The problem is that if you go to get a value from the settings file before it is loaded you will get an error. This comes back  to the whole top down approach we all got so used to. Just because you load the file a line above does not mean the file is ready to parse. After quickly discovering this problem  I realized that I was just using the wrong thought process. I remembered that everything is event driven in Flex and that is how I needed to start thinking. I came to the conclusion that before I could set my panel title I needed to know when the file was actually loaded and ready to read. This was pretty easy and all I needed to do was dispatch a custom event when the file was ready and listen for it In my main application.

So to write our configuration manager we are going to write 2 different classes. A ConfigManager class and a ConfigManagerEvent class. First lets look at the Config Manager class. The first thing we need to do is create a constructor that accepts a string as the settings file path.

Now that we know the path to our configuration file I will create a load config method that we can call from our main app. This should look pretty basic to most, also I have added some event listeners for things that might go wrong, but I am not implementing them in this example.

In our complete handler (when the file is loaded call this method) we know the file is loaded so we are going to set our data and dispatch our custom event. Our custom event just tells anyone listening that the config file is loaded and ready to be used. You will also notice I am setting the data for the event, this is so the xml data can be passed to the listener of this event.

Finally we have our custom event, this is pretty basic and just defines our event constants. We have a complete and fault event. All custom events extend the flash.events.Event class and this will look pretty basic to anyone who has created a custom event.

So now that we have used a different approach our implementation should make sense to you. I want to read the config file and listen for a complete event. In the event listener method we know the file is ready and we will have our data. We can get our data 1 of 2 ways. We already know that it is being passed in our event so we can grab it like that. I also like adding a nice helper method in our config manager class called get value that accepts the key name.

Here is the final code for our main application. I have an example of both ways you can get the title of the panel.

I hope this clears up some trouble you may have had. I think the important thing here is to really understand that you need to start shifting towards event driven programming when you get into Flex. If something does not make sense (or I screwed something up) let me know.

Comments

#1 Posted By: Johan Posted On: 8/21/08 5:59 PM
In your final example the first init() method on line 14 is exactly the same as the original - I do not see how the events you have added to the manager have changed anything. You set _title without checking a complete event. Does getVal() somehow know that the data is ready?

With the second init() method you need to pass the event carrying the data so where is the event listener that does this - I noted that creationComplete calls loadSettings() - maybe it happens there.

Anyway bottom line is I see where the complete event is dispatched but am missing where it gets used.
#2 Posted By: Dan Vega Posted On: 8/25/08 9:40 AM |
Author Comment
Wow, that is a big typo. I will fix it later today but basically you need to run init(1) and add an event listener for the 2nd method. I got all the code mixed up when I wrote this example.
#3 Posted By: alvicstep Posted On: 12/19/08 11:13 AM
I just tested the code. Here are the changes I made:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml";
   layout="vertical" creationComplete="init()">
   
   <mx:Script>
      <![CDATA[
         import org.vega.util.ConfigManagerEvent;
import org.vega.util.ConfigManager;
         
         [Bindable]
         private var _title:String;
         private var cm:ConfigManager;
         
         private function init():void
         {
            cm = new ConfigManager("config/settings.xml");
            cm.loadConfig();
            cm.addEventListener(ConfigManagerEvent.COMPLETE,ConfigManagerEventResultHandler);
         }
         
          private function ConfigManagerEventResultHandler(event:ConfigManagerEvent):void
          {
// event data
//_title = event.data["PanelTitle"];

// our helper method
_title = cm.getValue("basedir");
}
         
         
         
      ]]>
   </mx:Script>


Post Your Comment

Leave this field empty







Show Captcha

If you subscribe, any new posts to this thread will be sent to your email address.

Copyright © 2007 Dan Vega | BlogCFC was created by Raymond Camden. This blog is running version 5.8.001.