Solved

removing redundancy in mxml

Posted on 2011-03-04
3
363 Views
Last Modified: 2012-05-11
hi guys

I am new to flex so not sure how to solve this

I have 2 mxml files, both the mxml files have a peice of code which is repeated.

I want to avoid that. My 2 mxml are

1.developer.mxml

<mx:Script>
	<![CDATA[
		 
		[Bindable]
		private var user:Object;
		private function userHandler(event:ResultEvent):void 
		{
			user = event.result;
			
		}
	]]>
</mx:Script>

<mx:RemoteObject id="secHelper" destination="secHelper" >
<mx:method name="getAuthCredentials" result="userHandler(event)"/>
</mx:RemoteObject>

<mx:HBox width="100%" visible="{user.authority.contans(DEVELOPER)}" >	//using the user object here to check the role 	
		<mx:Button label="New Project Developer"/>		//button differs based on role  
	</mx:HBox>

Open in new window

now i have another mxml
2.manager.mxml which contains the same code just to check the role of manager.

<mx:Script>
	<![CDATA[
		 
		[Bindable]
		private var user:Object;
		private function userHandler(event:ResultEvent):void 
		{
			user = event.result;
			
		}
	]]>
</mx:Script>

<mx:RemoteObject id="secHelper" destination="secHelper" >
<mx:method name="getAuthCredentials" result="userHandler(event)"/>
</mx:RemoteObject>

<mx:HBox width="100%" visible="{user.authority.contans(MANAGER)}" >	//using the user object here to check the role 	
		<mx:Button label="New Project Manager"/>		//button differs based on role 
	</mx:HBox>

Open in new window

So clearly there is redundancy. I am repeating the <mx:RemoteObject>  and function userHandler(event:ResultEvent) in two places to get the user object. My goal is to have the code only once and reuse it.

Is that possible?

thanks
0
Comment
Question by:royjayd
  • 2
3 Comments
 
LVL 17

Expert Comment

by:deepanjandas
ID: 35044191
Yes This is Possible.
Create a Service mxml and place the Remote Object tag there as you have done here. Then place the handlers and dispatch custom events with the results.

Now in your developer and manager mxml add the service mxml like any other component and execute it by calling its public APIs that you need to declare in the Service mxml.

Hope this makes sense.

Warm Regards
Deepanjan Das
0
 

Author Comment

by:royjayd
ID: 35044203
>>>Hope this makes sense.

can you provide some code?  i am new to flex and still learning it so i cant understand 100% of what you said but i get the point. Some code would help.

thanks.
0
 
LVL 17

Accepted Solution

by:
deepanjandas earned 500 total points
ID: 35044275
Service.mxml
<?xml version="1.0" encoding="utf-8"?>
<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml"
		   width="400" height="300">
	<mx:Metadata>
		[Event(name="userData", type="flash.events.Event")]
	</mx:Metadata> 
	<mx:Script>
		<![CDATA[
			import mx.rpc.events.ResultEvent;
			import mx.rpc.events.FaultEvent;
			import flash.events.Event;
			
			
			[Bindable]
			public var user:Object;
			private function userHandler(event:ResultEvent):void 
			{
				user = event.result;
				
				var userEvent:Event = new Event( "userData" );
				dispatchEvent(userEvent);
				
			}
		]]>
	</mx:Script>
	<mx:RemoteObject id="secHelper" destination="secHelper" >
		<mx:method name="getAuthCredentials" result="userHandler(event)"/>
	</mx:RemoteObject>

</mx:Canvas>

Open in new window


Now in the developer and manager mxml add this:
<local:Service id="service" userData="onUserData(event)"/>

<mx:Script>
		<![CDATA[
			import flash.events.Event;
			
			private function onUserData(event:Event):void 
			{
				trace(service.user);
				
			}
		]]>
	</mx:Script>

Open in new window


Warm Regards
Deepanjan Das
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
AdvancedDataGrid 4 354
AS 3 - Movie editor 7 484
How to export vector graphics from Actionscript 3.0 SWF? 4 1,191
Adobe Flex - Combobox - Disable Editing 15 787
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…
How do we balance the user experience (UX) with reasonable security measures? It can be done, if you keep these fundamentals in mind.
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.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

786 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