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>
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>
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
Apache Flex
Last Comment
deepanjandas
8/22/2022 - Mon
deepanjandas
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
royjayd
ASKER
>>>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.
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