Solved

removing redundancy in mxml

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

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…
Short answer to this question: there is no effective WiFi manager in iOS devices as seen in Windows WiFi or Macbook OSx WiFi management, but this article will try and provide some amicable solutions to better suite your needs.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

747 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

13 Experts available now in Live!

Get 1:1 Help Now