Understanding cairngormevents in action script
I've got a drop down that is populated asynchronously by a web service with a caringorm event. I need to know when this event completes so I can set the selected combo box row. The data gets populated but I can't figure out how to subscribe to this event. Does anyone know hot to do this.
This code doesn't appear to work.
CairngormEventDispatcher.getInstance().addEventListener(SomeEvent.GETDATA, formLoaded ); new SomeEvent(SomeEvent.GETDA开发者_Python百科TA).dispatch();
protected function formLoaded(event : CairngormEvent):void{ Alert.show("Woot"); }
Cairngorm is a MVC microarchitecture.
first of all: here's the documentation for cairngorm 2.2.1 http://cairngormdocs.org/docs/cairngorm_2_2_1/index.html
A CairngormEvent is associated to an ICommand by the method addCommand of FrontController class. So whenever you execute SomeEvent(SomeEvent.GETDATA).dispatch() you're actually executing the execute() method of the command associated to SomeEvent.GETDATA by the controller.
Listening for events from CairngormEventDispatcher is not a best practice. You should not call CairngormEventDispatcher in any case, that class is there in order to make the whole microarchitecture run properly.
Your view (the combobox) should react to the model (the result of the webservice) automatically (or via some other object in the middle [as puremvc does])
What I suggest to quickly solve your problem using cairngorm is:
register the webservice into the service locator in order to have an application level reference to it (to do this, just create an mxml component extending ServiceLocator, and put your httpservice or webservice tag in there, then assign an id to it)
create your model locator (the class which stores the data retrieved from the web) and mark your data list you want to display in the combobox as [Bindable]
identify the command coupled with SomeEvent.GETDATA and:
- call the service registered in the service locator in the execute method
- implement IResponder interface (http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/mx/rpc/IResponder.html) in order to handle the result of the service (register your command as responder of the service in the execute method)
in the result method parse the data you had back from the server, then store them into the model locator (which can be easily retrieved via the singleton accessor)
in the view (the mxml file holding the combo), bind the combo dataprovider to your model locator data list (made bindable few lines before) in order to make it react to the changes occurred in the model.
have fun ;)
精彩评论