Solved

creating flex dictionary , urgent

Posted on 2011-03-01
7
584 Views
Last Modified: 2012-05-11
here it goes, I am getting the result object from backend through blazeds . form this result object i was able to populate the data into advanced datagrid . there will be a  id for each message. The same message will have 2 states i.e.initialize and complete. when we get the same message with complete status , we have to update the initialize to complete, so we do not populate the same message with two states. for this purpose I want to create a dictionary which will hold the key  of the conversation ids adn values of the message.

pls suggest me how to implement this?
0
Comment
Question by:arigs
  • 4
  • 3
7 Comments
 
LVL 17

Accepted Solution

by:
deepanjandas earned 500 total points
ID: 35013901
The best is to create a Dictionary Object in Flex like this having the keys and values in an xml:
The Dictionary class:
package
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.text.Font;
	import flash.utils.Dictionary;

	public class DictionaryManager extends EventDispatcher
	{
                private static var instance : DictionaryManager;
                public static const STATIC_DICTIONARY_PATH:String = "configs/dict_static.xml";
		public static const LOADED:String = "dictionaryLoaded";

		private var dictionary:Dictionary;
		
		public function DictionaryManager( enf:SingletonEnforcer )
		{
			dictionary = new Dictionary();
		}
		
		public static function getInstance() : DictionaryManager 
		{
			if( instance == null ) instance = new DictionaryManager( new SingletonEnforcer() );
			return instance;			
		}
		
		public function set xml( path:String ):void 
		{
			var urlLoader:URLLoader = new URLLoader();
			urlLoader.addEventListener( Event.COMPLETE, onXMLLoadCompleteHandler );
			urlLoader.addEventListener( IOErrorEvent.IO_ERROR, onXMLLoadErrorHandler );
			urlLoader.load( new URLRequest( path ) );
		}
		
		private function onXMLLoadCompleteHandler( event:Event ) : void
		{
			EventDispatcher( event.currentTarget ).removeEventListener( event.type, arguments.callee );
			
			var urlLoader:URLLoader = URLLoader( event.target );
			var xml:XML = new XML( urlLoader.data );
			urlLoader.close();
			urlLoader = null;
			
			parse( xml );
		}
		
		private function onXMLLoadErrorHandler( event:IOErrorEvent ) : void
		{
			//
		}
		
		public function parse( config:XML ) : void
		{
			var itemList:XMLList = config.item;
			for each ( var prop:XML in itemList ){
				dictionary[String( prop.@id )] = String( prop.translate );
			}

                        var loadedEvent:Event = new Event( DictionaryManager.LOADED );
			dispatchEvent( loadedEvent );
		}
		
		public function get( value:String ) : String
		{
			return ( dictionary[value] == null ) ? value : dictionary[value];
		}
	}
}
internal class SingletonEnforcer{}

Open in new window

//use this in your code
var dictionaryManager = DictionaryManager.getInstance();
dictionaryManager.addEventListener( ResourceManager.LOADED, onDictionaryLoaded, false, 0, true );
dictionaryManager.xml = DictionaryManager.STATIC_DICTIONARY_PATH;

private function onDictionaryLoaded( event:Event ):void
{
	dictionaryManager.removeEventListener( DictionaryManager.LOADED, onDictionaryLoaded );
			
}

Open in new window

The xml:
//sample xml
<?xml version="1.0" encoding="UTF-8"?>
<dict lang="en">
	<item id="key1"><translate><![CDATA[Value1]]></translate></item>
	<item id="key2"><translate><![CDATA[Value2]]></translate></item>
	<item id="key3"><translate><![CDATA[Value3]]></translate></item>
  	<item id="key4"><translate><![CDATA[Value4]]></translate></item>
  	<item id="key5"><translate><![CDATA[Value5]></translate></item>
</dict>

Open in new window


Warm Regards
Deepanjan Das  
0
 

Author Comment

by:arigs
ID: 35013952
Hi Deepan,

          Thank you for ur reply and valuable time. I am using the cairngorm , so can i declare a variable of dictionary in modellocator? in that case, is what you have written  applicable, still ?
0
 
LVL 17

Expert Comment

by:deepanjandas
ID: 35013990
Yes, it is applicable for any actionscript 3.0 project.

In your model locator declare the DictionaryManager in the constructor as :
_dictionaryManager = DictionaryManager.getInstance();

Open in new window


Then create a getter to be accessible in all places in your application like this:
public function get dictionaryManager() : DictionaryManager
{
	return _dictionaryManager;
}

Open in new window



In your main mxml where you initialized your ModelLocator, write the following code before initializing your application:
//use this in your code
var model:ModelLocator = ModelLocator.getInstance();
model.dictionaryManager.addEventListener( ResourceManager.LOADED, onDictionaryLoaded, false, 0, true );
model.dictionaryManager.xml = DictionaryManager.STATIC_DICTIONARY_PATH;

private function onDictionaryLoaded( event:Event ):void
{
        model.dictionaryManager.removeEventListener( DictionaryManager.LOADED, onDictionaryLoaded );
                        
}

Open in new window


Now you can access your dictionary from anywhere like this:
model.dictionaryManager.get("key1");

Open in new window


Warm Regards
Deepanjan Das  
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:arigs
ID: 35018174
Hi Deepanjan,

                         If You don't mind can you pls give your email id, so i can send screenshots of my project which will give clear picture of my problem.

Thanks
0
 
LVL 17

Expert Comment

by:deepanjandas
ID: 35018720
I am not sure if its right to provide emails here, Kindly access my email through my blog in the link http://www.experts-exchange.com/M_6015623.html from my personal blog.

Warm Regards
Deepanjan Das
0
 
LVL 17

Expert Comment

by:deepanjandas
ID: 35018722
Also you may send the screenshots here as file attachments.

Warm Regards
Deepanjan Das
0
 

Author Closing Comment

by:arigs
ID: 35070044
gr8 approach
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

First things first - Preparation We need all the part for this install and it's much nicer to have them all on hand when you need them so here's what's required. Download Eclipse 3.5 32 bit (I like the Classic flavour) from here. (http://www.e…
A Short Story about the Best File Recovery Software – Acronis True Image 2017
This video discusses moving either the default database or any database to a new volume.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now