开发者

how can I use the event parameter when the code is written inline in the mxml tag in flex

开发者 https://www.devze.com 2023-03-10 08:17 出处:网络
What is the difference between : giving a function reference in the result attribute of the HTTPService class like this :

What is the difference between :

giving a function reference in the result attribute of the HTTPService class like this :

protected function httpserviceCheckUpdate_resultHandler(event:ResultEvent):void
{
     var version:Number = event.result.text.version as Number;
     var xmldata:XML = XML(event.result);
     // and all the processing you want to do
}
<mx:HTTPService id="httpserviceCheckUpdate"
                    method="POST"
                    url="http://abc.com/actions.php"
                    resultFormat="text"
                    fault="httpservicexml_faultHandler(event)"
                    result="httpserviceCheckUpdate_resultHandler(event)">
        <mx:request xmlns="">
            <action>versionCheck</action>
        </mx:request>
    </mx:HTTPService>

and

coding the complete function in the <mx:result></mx:result> of the same HTTPService class.

<mx:HTTPService id="httpserviceCheckUpdate"
                method="POST"
                url="http://abc.com/actions.php"
                resultFormat="text"
                fault="httpservicexml_faultHandler(event)"
                result="httpserviceCheckUpdate_resultHandler(event)">
    <mx:request xmlns="">
        <action>versionCheck</action>
    </mx:request>
    <mx:result>
        <![CDATA[
        var version:Number = event.result.text.version as Number;
        var xmldata:XML = XML(event.result);
        // and all the processing you want to do
        ]]>
    <开发者_如何学编程/mx:result>
</mx:HTTPService>

If both of them are same then how can I provide the event parameter using the second approach (as written in the result attribute of the httpservice class)?

Which one of them is the better way of doing things in FLEX

Thanks


Using the <mx:result> tag is not a standard way of doing things. You can use tags for any events using MXML. It's made for quick prototyping but not code cleanliness. They're not exactly the same since the latter is an anonymous function with no parameters, however you can access the result using the httpserviceCheckUpdate.lastResult property.

Your first approach is the better one because it's the standard, it's cleaner, resuable and you can set the parameters.


Well actually I mostly set events in the code, so not even like your first example but, say, in a complete event calling:

component.addEventListener(<event>, <handler>, <usecapture>, <priority>, <weakreference>)

always using all 5 parameters. I do it in code since I tend to remove listener when they are not needed but then add them again when needed: this can only be done in code.

I use always all 5 parameters because it allows a fine control over the priorities (who gets the event first), second because of memory management: if I by any chance forget to remove a listener and the eventdispatcher is the only strong reference to the event handler the garbage collector will still be able to remove the object the handler belongs to. I think this alone is worth defining event handlers in code.

Back to you question: I have never found an example like yours second, what I found is something like this:

<mx:HTTPService id="httpserviceCheckUpdate"
                method="POST"
                url="http://abc.com/actions.php"
                resultFormat="text"
                fault="httpservicexml_faultHandler(event)"
                result="httpserviceCheckUpdate_resultHandler(event)">
    <mx:request xmlns="">
        <action>versionCheck</action>
    </mx:request>
    <mx:result>
        <![CDATA[
        import ....ResultEvent;
        protected function httpserviceCheckUpdate_resultHandler(event:ResultEvent):void 
        {
            var version:Number = event.result.text.version as Number;
            var xmldata:XML = XML(event.result);
            // and all the processing you want to do
        }
        ]]>
    </mx:result>
</mx:HTTPService>

as you see you have a full function signature including Event here. The only difference is that handlers defined within a component will be called before those defined in the container, other than that I see them as equally good and both worse than setting handlers in code.

0

精彩评论

暂无评论...
验证码 换一张
取 消