Solved

creating flex dictionary , urgent

Posted on 2011-03-01
7
594 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Giving access to ESXi shell console is always an issue for IT departments to other Teams, or Projects. We need to find a way so that teams can use ESXTOP for their POCs, or tests without giving them the access to ESXi host shell console with a root …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

839 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