在AS3中,如何动态地向WebService对象添加操作?

时间:2013-01-04 14:28:22

标签: web-services actionscript-3 flex actionscript mxml

假设您在MXML中的fx:Declarations标记内定义了一个WebService对象,其中包含一些操作,以及这些操作的响应和错误处理程序。现在假设您要将该WebService传递给另一个对象的构造函数,然后该对象将在运行时向WebService添加一个操作,以及它自己的响应和该新操作的错误处理函数。

例如:

<fx:Declarations>
    <s:WebService id="ws" fault="Alert.show('failure')">
        <s:operation
            name="Op1"
            resultFormat="object"
            result="WebOp1(event);"
            fault="WebFaultOp1()"
        />
    </s:WebService>
</fx:Declarations>

.
.
.
        var a:A = new A(ws);

public class A
{
    private var m_ws:WebService;

    public function A(pWS:WebService)
    {
        m_ws = pWS;
        m_ws.Op2 = new Operation();
        m_ws.Op2.resultFormat = "object";
        m_ws.Op2.result = WebOp2(event);
        m_ws.Op2.fault = WebFaultOp2(event);
    }

    private function WebOp2(pEvent:ResultEvent):void
    {
    }

    private function WebFaultOp2(pEvent:FaultEvent):void
    {
    }
}

怎么可以这样做?如果必须的话,我愿意使用MXML,但我真正想要避免的是必须创建两个独立的WebService对象,它们只共享相同的WSDL。谢谢!

1 个答案:

答案 0 :(得分:0)

尝试这样做:

var operation:Operation = new Operation(webService, 'noName'); // mx.rpc.soap.mxml.Operation
operation.addEventListener(ResultEvent.RESULT, onResult);
operation.addEventListener(FaultEvent.Fault, onFault);
webService.operations['noName']=operation;
相关问题