Hi!
In my flex application I use services a lot. I need them to update my data in the application, so call them quite often. Currently I implemented it in the following way:
1) There is a service provider (AMFLoader class) 2) And response handler (Responder class)
The code looks like this:
public function AMFLoader(url:String):void
{
gateway = new NetConnection();
gateway.connect(url);
}
public function callAMFLoader(serviceName:String, param:String, resultHandler:AMFResultHandler):void
{
gateway.addEventListener(IOErrorEvent.IO_ERROR, resultHandler.onIENetError);
gateway.addEventListener(NetStatusEvent.NET_STATUS, resultHandler.onNetStatus);
responder = new Responder(resultHandler.onSuccess, resultHandler.onFalse);
gateway.call(serviceName,responder, param);
}
and Result handler class:
public class AMFResultHandler
{
public function AMFResultHandler()
{
}
public function onSuccess(result:*):void
{
trace("Result from basic AMF Result handler on success: " + result);
}
To make a call to a service I extend basic Result handler class, and make custom functions to process results. Usually on the latest stages, I'm binding data received from the service to the global variables which I defined in the main application, and then use it in other classes.
Currently I noticed that this is quite an annoying thing to use global variables, not sure if it's a good programming style to do it. For example when I tried to refactor my code, I noticed that it's sometimes hard to understand who and when (and why), populated data there.
Maybe you can suggest a way of storing the data from services calls. The main thing I am failing to understand is that we can't just create a method, e.g. getData, and call it somewhere in the application, because calling service and storing the data are different things (as data is received only on some event)....
Thanks in advance.