AS3 ResourceMap API

Friday, February 8th, 2008

Of the many APIs I have developed and published as Open Source to the Flex community, the AS3 HashMap has been one of the most popular. My assumption is that this popularity is in part due many developers expecting a HashMap to be provided as part of the Flex Framework, and rightfully so. After all that is why I developed it in the first place.

With the upcoming release of Adobe Flex 3 there are many new additions which have been added to the Framework. These new additions and features are outside the scope of this article; however, one which is of relevance is the ability to directly access the underlying content of a ResourceBundle.

For the most part I tend to think of the content of a ResourceBundle as not begin much different than a HashMap as it essentially is just an aggregation of name / value pairs, just like a HashMap. With that in mind I have developed a ResourceMap API which allows developers to work with a ResourceBundle via an IMap implementation (a.k.a. HashMap).

ResourceMap implements the IMap interface allowing the underlying content of a ResourceBundle to have CRUD specific operations performed on it. To utilize the ResourceMap one only need to instantiate an instance of ResourceMap and pass in a ResourceBundle, as in the following:

The above example is all that is required to work with a Resourcebundle as a HashMap. From there you can work with the ResourceBundle just as you would with a typical HashMap.

The benefit to this approach is it allows developers to dynamically add, remove, update and delete resources at runtime, whereas the new Flex 3 ResourceManager (much like the Open Source ResourceManager API I created last year – had to add that in) does not provide an API for setting resources. In addition, all of the getters defined by the IResourceBundle interface and implemented by ResourceBundle have been deprecated in favor of the new ResourceManager. However, IResourceBundle now provides an additional operation called getContent(); which exposes a reference to the underlying content Object which is created when a .properties file is compiled. Therefore it is possible to take advantage of this by accessing the content object.

Admittedly the thinking behind the ResourceMap API takes a somewhat “outside-of-the-box” approach to working with ResourceBundles as one typically tends to think of resources as constants, especially when working with localized applications. However with all of the new capabilities available in the Flex 3 Resource API (such as loading compiled resource swf’s at runtime, etc) the opportunity to experiment with different things is well worth it!

ResourceMap is published under the MIT license.

{ Comments are closed! }