Solved

creating flex dictionary , urgent

Posted on 2011-03-01
7
593 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

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…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

803 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