View Full Version : Handling HttpService results
suresh13
02-13-2009, 10:40 PM
Hi Everyone,
I am quite new to Flex development. I am trying to separate my flex code into different layers using the MVC pattern.
I have a button in my MXML when the user clicks on it, a request is sent to the Controller.as which in turn calls the DataHandler.as to get the Data.
The DataHandler.as class acts as a service and talks to the Java backend to get the data. I am using BlazeDS to talk to the java.
In my DataHandler.as class, I have a HttpService which sends a request to a Java Servlet to get data. As these calls are asynchronous, I am not sure how to handle the result and send data back to the controller.as which actually made the request.
Here is the psuedo code.
DataHandler.as
// sends a request to a java servlet to get data
function getData() {
httpService.send();
}
// handles the httpservice result
function handleResult() {
}
Any help is appreciated. Thanks in advance.
wvxvw
02-13-2009, 11:36 PM
You just need to add event listener to the HTTPService :)
I'd usually say: "Print out and frame Controller.as and DataHandler.as and hang them on the wall..." but I don't want to sound impolite, so, please, don't get me wrong. HTTPService alone is already a controller, no need for additional controllers and data handlers, it already handles the data, dispatches event with the data already after it's been parsed, sorted etc... if you need different kind of parsing / sorting - extend the HTTPService, everything above it is nothing but architectural extravagances :)
suresh13
02-16-2009, 04:22 PM
thanks for the reply. I did add an event listener for the HttpService, however when I make a call to the data handler.as from my controller.as it gives me no data as the httpservice calls are asynchronous. Do I need to constantly loop thru and check if there is a result and return the result to the controller.
The reason for separating the data handler code is to use MVC architecture, I dont want all of my code in just one class. Any help is appreciated.
wvxvw
02-16-2009, 04:43 PM
This is what the whole idea of listeners is meant for: flash does not execute loading synchronously -> you add listeners and throw away your controller and dataHandler :) Listener will be called when data arrives -> you handle data arrival inside the listener - as simple as it.
suresh13
02-16-2009, 05:08 PM
thanks again. Yeah I know it works If I have my httpService call and result handling code all in the MXML or all in one .as file. As I am new to Flex programming, Im not sure if it is a good coding practice to put all the code in one file.
wvxvw
02-16-2009, 07:02 PM
Actually, it has nothing to do with the number of files you have, but, there usually are more then one :) What I mean, is that this would be an overhead, and you really need additional controller when you have a lot of different services, but you surely don't need the additional controller for only one service because it already a controller.
Though, I prefer the event-map pattern over hierarchical triads, because IMO, having view, model and controller for every single component is an overkill.
suresh13
02-16-2009, 09:03 PM
hey thanks for all the responses. Say I have to do different operations: to add, delete, update and get data. Do I need to have different HttpServices for these operations or can I use the same httpService and use parameters to distunguish each operation and handle unique result handlers for each operation.
Also if we have multiple services, do you suggest to have mutiple controllers?
wvxvw
02-16-2009, 09:43 PM
I believe that if you reset the url property and call send() on the service, then you can use the same service to connect to different destinations, and well, you can always set / reset the handlers / pass different arguments in the send() / modify the request object etc... though I don't really see how would you delete that data using webservice, that's totally up to you what you do with data after you receive it...
|
vBulletin® v3.8.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.