Invocando métodos remotos dinamicamente utilizando RemoteObject
Twitter!
Muitos sabem como invocar um método utilizando RemoteObject, tanto no mxml quanto em uma classe ActionScript, mas com fazer se o nome do método é resolvido dinamicamente como, por exemplo, chamar um método que o nome seja o valor do texto de um TextInput?
No exemplo abaixo vou mostrar como se faz tanto utilizando a classe mx.rpc.remoting.mxm.RemoteObject, usado em um mxml e
mx.rpc.remoting.RemoteObject, usado em uma classe ActionScript.
Segue o código:
CODE:
-
<?xml version="1.0" encoding="utf-8"?>
-
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
-
initialize="initApp(event)">
-
<mx:Script>
-
<![CDATA[
-
import mx.controls.Alert;
-
import mx.events.FlexEvent;
-
import mx.rpc.events.FaultEvent;
-
import mx.rpc.events.ResultEvent;
-
-
private var remoteCallAS:RemoteObject;
-
-
private function initApp(event:FlexEvent):void
-
{
-
remoteCallAS = new RemoteObject("remoteCall");
-
remoteCallAS.showBusyCursor = true;
-
remoteCallAS.addEventListener(ResultEvent.RESULT, resultHandler);
-
remoteCallAS.addEventListener(FaultEvent.FAULT, faultHandler);
-
}
-
-
private function resultHandler(event:ResultEvent):void
-
{
-
Alert.show("resultado chegou!");
-
}
-
-
private function faultHandler(event:FaultEvent):void
-
{
-
Alert.show("chamada falhou!");
-
}
-
]]>
-
</mx:Script>
-
-
<mx:RemoteObject id="remoteCall" destination="remoteCall"
-
result="resultHandler(event)" fault="faultHandler(event)"
-
showBusyCursor="true" />
-
-
<mx:HBox>
-
<mx:Label text="Nome do método" />
-
<mx:TextInput id="methodName" text="teste" />
-
</mx:HBox>
-
-
<mx:RadioButtonGroup id="roGroupSelector" />
-
-
<mx:RadioButton groupName="roGroupSelector"
-
label="Usando mx.rpc.remoting.mxml.RemoteObject" value="0" selected="true" />
-
<mx:RadioButton groupName="roGroupSelector"
-
label="Usando mx.rpc.remoting.RemoteObject" value="1" />
-
-
<mx:Button label="invocar">
-
<mx:click>
-
<![CDATA[
-
roGroupSelector.selectedValue == 0 ?
-
remoteCall[methodName.text].send('param1', 'param2') :
-
remoteCallAS[methodName.text].send('param1');
-
]]>
-
</mx:click>
-
</mx:Button>
-
-
</mx:Application>

