Flex user defined runtime settings Part 2
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.
public class ConfigManagerEvent extends Event { public static const COMPLETE:String = "Complete"; public static const FAULT:String = "Fault"; public var data:Object; public function ConfigManagerEvent(type:String, bubbles:Boolean=false, cancelable:Boolean=false) { super(type, bubbles, cancelable); } } }
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.
