views:

241

answers:

3

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.

+1  A: 

There are several Flex frameworks that can help you:

Each of these frameworks will help you better organize your models and services. Take a look at each of them and choose one that you're comfortable with.

Mike Sickler
+1  A: 

What you're asking about is one of the main purposes of an MVC framework. Specifically your talking about the Model or data interface. What you should do, if you don't want to go with a full blown framework just yet, is create 2 classes.

1) DataObject

2) DataModel

where "Data" is replaced with something intuitive like User or Products. In the DataObject you keep only properties of the data and similarly it is the only place where data is assumed to be correct. The DataObject though is only accessed directly through the DataModel. Inside the data model you keep all the getters and setters of the DataObject as well as any remote service calls you need to populate the DataObject.

This is just the beginning of MVC style application development and if you truly want to move on to more flexible, manageable code you should consider going with a full blown framework. Two that i recomend are:

1) Swiz ( http://swizframework.org/ ) : a super simple event driven framework that really leverages the power of flex.

2) PureMVC ( http://puremvc.org/ ) : an easy to understand MVC code structure that emphasizes code decoupling and strict MVC architecture.

I say start with one of those two and you'll be well on your way to becoming a better flex developer.


a couple great tutorials:

swiz: http://vimeo.com/7166397

pureMVC: http://active.tutsplus.com/tutorials/workflow/understanding-the-puremvc-open-source-framework/

greg
Diving in...So see you after it :) Hope I will get used to use it quickly
Oleg Tarasenko
A: 

I personally like Mate as well. I'm really digging their framework and am retro-fitting it for a few projects I'm currently working on right now.