Log In  
  Getting Started  
----------------------------------------

Resources

 

Simple UI Event

Example 1

  • A click event is created by clicking the button
  • The click event triggers the event handler

Example 2

  • A click event is created by clicking the button
  • The click event triggers an asynchronous HTTPService call
  • The HTTPService call waits for a response
  • The result event (response) triggers the event handler
previous page next page

User rating?

Learn more
  • ActionScript code needs to be embedded in CDATA when used in MXML
  • The import mx.controls.Alert line makes it possible to reference Alert directly without having to use the fully qualified name. Otherwise, the alert call would look like this: mx.controls.Alert.show("Clicked!");

Example1.mxml

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
    <mx:Script>
        <![CDATA[
            import mx.controls.Alert;
            private function myHandleClick(event:Event):void
            {
                Alert.show("Clicked!");
            }
        ]]>
    </mx:Script>
    <mx:Button id="myButton" label="Create Click Event" click="myHandleClick(event)" />
</mx:Application>

Rendered sample

Example 2

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
    backgroundColor="#FFFFFF"
    backgroundAlpha="0">

    <mx:HTTPService
        id="twitterService"
        url="http://twitter.com/statuses/public_timeline.xml"
        resultFormat="e4x"
        result="twitterServiceResultHandler(event)" />

    <mx:Script>
        <![CDATA[
            import mx.rpc.events.ResultEvent;

            private function twitterServiceResultHandler(event:ResultEvent):void {
                resultTxt.text = event.result.toString();
            }
        ]]>
    </mx:Script>

    <mx:Button id="myButton"
        label="Send HTTP Request"
        click="twitterService.send()" />
    <mx:TextArea id="resultTxt"
        fontSize="12"
        width="100%" height="100%" />

</mx:Application>

Rendered sample

previous page next page

Great tutorial!

One problem though, I get the following error when trying example 2:

[RPC Fault faultString="Security error accessing url" faultCode="Channel.Security.Error" faultDetail="Destination: DefaultHTTP"]
 at mx.rpc::AbstractInvoker/http://www.adobe.com/2006/flex/mx/internal::faultHandler()
 at mx.rpc::Responder/fault()
 at mx.rpc::AsyncRequest/fault()
 at DirectHTTPMessageResponder/securityErrorHandler()
 at flash.events::EventDispatcher/dispatchEventFunction()
 at flash.events::EventDispatcher/dispatchEvent()
 at flash.net::URLLoader/redirectEvent()

Correct. That second example relies on a crossdomain.xml file on twitter.com that is open. Unfortunately, twitter had to restrict access to its site's services.

There is more about this in the thread here:
http://groups.google.com/group/twitter-development-talk/browse_thread/thread/8d09970f449abc70

You'll note that if you copy the code from this example and run it locally on your machine, it should work. However, when you deploy it, it will result in a security error. One workaround is to use a proxy page (in PHP or JSP or ASP.NET or whatever) that you call with the HTTPService tag instead of the twitter page. On that proxy page, you make the call to twitter.

hth,
matthew j. horn
flex docs

Added by Mark Nichoson , last edited by matthew horn on Apr 22, 2008  (view change)
Labels: 
(None)

Powered by Atlassian Confluence 2.7.1, the Enterprise Wiki. Bug/feature request - Atlassian news - Contact administrators