?
Solved

removing redundancy in mxml

Posted on 2011-03-04
3
Medium Priority
?
374 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

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…
IF you are either unfamiliar with rootkits, or want to know more about them, read on ....
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

765 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