Solved

removing redundancy in mxml

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
A simple overview of the possibilities of using technology for project management.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

863 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

20 Experts available now in Live!

Get 1:1 Help Now