Flex 2 WebService & Cairngorm 2 Example

*** Update: 8.14.2007: The code in this article is out of date. Please refer to the new entry instead. ***

ws_icon.jpgThis example utilizes Amazon’s search webservice in the Cairngorm 2 application framework. You can go to Amazon.com and register for a free API key. Once you do, you just plug into this example’s constant file, and she’ll work for you too if you when you compile. This example application allows you to search for books on Amazon.com by keywords.

The events are debatable in their place in the package structure. They extend CairngormEvent, thus implying they fire commands, but you could reuse events. Also, I am not very good at E4X so please do not use that as an example of E4X usage.

Here’s basically how she works:

  1. App boots up, shows first state
  2. user types in some keywords, clicks the Search button
  3. this fires the doSearch event handler
  4. the doSearch handler creates a CairngormEvent, and dispatches it
  5. the Controller has mapped that type of event to a Command, and thus it runs the command, passing it the event
  6. the Command looks at the event type in the execute function and decides what to run, in this case, searchBooks, passing the event’s data to the function
  7. search books creates the Business Delegate, and calls its method
  8. the Delegate gets the WebService from the ServiceLocator, fires off the WebService, and waits for a response
  9. if the response is successful, it parses the E4X XML via a Factory to a list of Book ValueObjects
  10. finally, it calls the responder’s onResult function
  11. Since the Command is the Responder, he gets the data, and sets it to the ModelLocator and updates the applications state
  12. The View’s are bound to the state variable and book list in the ModelLocator, so they immediately have their bindings fire
  13. This causes the main app to change state, and show the results DataGrid
  14. The results DataGrid is bound to the books list in the ModelLocator, and shows the results.

* Note: I haven’t got my serial for Flex Builder 2 yet, so the App & View Source will time out after a day. The source, however, is good forever.

Flex 2 WebService & Cairngorm 2 Example – App | View Source | Source ZIP

BTW, I fixed my Atom feeds, thanks to those who let me know they were busted. If I don’t know it’s broke, I can’t fix it.

27 Replies to “Flex 2 WebService & Cairngorm 2 Example”

  1. That’s an interesting note regarding expriration of SWF files after 1 day compiled by the help of Flex Builder 2.

    What will happen if we will compile SWF file using Flex 2 command-line compiler ( via Ant script, for example)

    In my understanding, because Flex 2 SDK is free, those SWF compiled from command line should last for ever w/o time bomb put inside.

  2. Commandline compiler SWF’s do not timeout.

    I think SWF’s created with a trial version of Flex Builder 2 do, though.

  3. Others have reported this problem. It has been suggested that perhaps I’m compiling for an earlier build of the player. I haven’t had time to check this yet, but if you compile yourself, she should work just fine.

  4. I’m getting the following error when submiting the search term. Any idea what may be happning?

    TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at com.jxl.amazon.factories::AmazonFactory/buildArrayOfBooksFromXML()
    at com.jxl.amazon.business::SearchDelegate/com.jxl.amazon.business:SearchDelegate::searchBooks_onResult()
    at flash.events::EventDispatcher/flash.events:EventDispatcher::dispatchEventFunction()
    at flash.events::EventDispatcher/dispatchEvent()
    at mx.rpc::AbstractService/dispatchEvent()
    at mx.rpc.soap.mxml::WebService/dispatchEvent()
    at mx.rpc::AbstractOperation/http://www.adobe.com/2006/flex/mx/internal::dispatchRpcEvent()
    at mx.rpc::AbstractInvoker/http://www.adobe.com/2006/flex/mx/internal::resultHandler()
    at mx.rpc::Responder/result()
    at mx.rpc::AsyncRequest/acknowledge()
    at ::DirectHTTPMessageResponder/completeHandler()
    at flash.events::EventDispatcher/flash.events:EventDispatcher::dispatchEventFunction()
    at flash.events::EventDispatcher/dispatchEvent()
    at flash.net::URLLoader/flash.net:URLLoader::onComplete()

  5. Great resource Jess!

    Just a small note that caused me a little grief to help those new on the learning track to avoid. If you’re converting this to a HTTPService app, make sure to call

    ServiceLocator.getInstance().getHTTPService(‘ServiceNameGoesHere’) as HTTPService

    instead of

    ServiceLocator.getInstance().getService(‘ServiceNameGoesHere’) as WebService

    in your SearchDelegate.as file after adding the HTTPService in the Services.as file. Otherwise it will give you a result event type casting error.

  6. yes..Jester! That was it. Shame on me for not reading that at the top. Usually when I see an example, I like to quickly compile and test if it works before analyzing it. Works like a charm. Look forward to breaking it down. BTW, how do you use the DebugWindow with it??
    Jeremy

  7. First, uncomment line 42:

    debug_win = PopUpManager.createPopUp(this, DebugWindow, false) as DebugWindow;

    Second, type something like:

    DebugWindow.debug(‘Hola, que tal!’);

  8. Every time I run I get the same error as boybles was getting. I made sure that I have my amazon key which seemed to resolve boybles problem, but I still get the same error (see below)… any ideas?

    TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at com.jxl.amazon.factories::AmazonFactory/buildArrayOfBooksFromXML()
    at com.jxl.amazon.business::SearchDelegate/com.jxl.amazon.business:SearchDelegate::searchBooks_onResult()
    at flash.events::EventDispatcher/flash.events:EventDispatcher::dispatchEventFunction()
    at flash.events::EventDispatcher/dispatchEvent()
    at mx.rpc::AbstractService/dispatchEvent()
    at mx.rpc.soap.mxml::WebService/dispatchEvent()
    at mx.rpc::AbstractOperation/http://www.adobe.com/2006/flex/mx/internal::dispatchRpcEvent()
    at mx.rpc::AbstractInvoker/http://www.adobe.com/2006/flex/mx/internal::resultHandler()
    at mx.rpc::Responder/result()
    at mx.rpc::AsyncRequest/acknowledge()

  9. When I try to run the app it gets to the service.ItemSearch() call in the searchDelegate and then gets a fault. But when I look at the fault event I can’t seem to figure out what the error is. The message text and message id are both null. I got a key from amazon and put that in so I don’t believe that is the issue. The faultString says something about Actor not supplied but is required. Actor seems to be required for some other method not the one being called by this example. If I let it continue nothing happens.

    Any help would be appreciated.

    Thanks!

  10. I can duplicate this error in my Flex 2.01. So far, no documentation nor Google results can be found about this specific error. Still digging…

  11. I have an Urgent need for a Perm. Sr. Applications Developer/Flex Architect in the Dallas Texas area, would you happen to know of anyone with that experience that would be interested? If so please let me know.

    Kim Dobson
    Placement Manager
    214-277-2097

  12. I’m getting the same fault that John G. has reported above, with the same faultString Has anyone had any luck in figuring it out? Google still doesn’t show any relevant info.

    Thanks.

  13. Thanks for posting the sample, its very helpful for me. I am getting the same error reported by John G in Flex Builder 2.0.1

  14. Fantastic, thank you so much for the example! I’m all new to Cairngorm, and couldn’t find the Delegate class anywhere, nor could I figure out how to transfer the example in the tutorial (which uses a RemoteObject) to a HTTPService, so I got stuck on the business classes. Your example saved the day! :-)

  15. Same fault event Apparently something has changed in the 2.01 framework. It is not a sandbox issue as I get the same fault event in an Apollo test. Apollo has signifigantly less sandbox restrictions.

Comments are closed.