Invocando métodos remotos dinamicamente utilizando RemoteObject

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?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>

Nenhum comentário

Deixe Seu Comentário