Solved

removing redundancy in mxml

Posted on 2011-03-04
3
365 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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
what can cause a flash page to take 30 sec. to display? 2 467
regular expression:- validate string of numbers 1 425
Button sound on mouseover 4 477
adobe air 1 301
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 quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

829 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