8/4/2010 3:43:01 AM
Title:
Need Help in remote Object with addEventListner
Hi,
Need one help in flex 4.
Searched all over internet around 3 days, Couldn't find it..
here is my question,
Consider, I need to fetch data from java by using remote object.
for that, i need to create 2 funcrion a) Invoke the remote object b) Uising addEventListner & resultEvent to fetch the data as arrayCollection.
i.e
public function remoteHandler():void
{
var ro:RemoteObject= new mx.rpc.remoting.mxml.RemoteObject;
ro.destination="javaDestination";
ro.getJavaValue();
ro.addEventListener(ResultEvent.RESULT,resultHandler);
}
private function resultHandler(event:ResultEvent):void
{
var objectiveList:ArrayCollection= new ArrayCollection;
objectiveList=event.result as ArrayCollection;
}
I am pretty sure above code is work,
But i need to combine these 2 functions together and make a single function which return the arryCollection, So that i can call this function is some loop and fetch the java values whenever i need.
But i am not able to combine there 2 functions since addEventListner returning null value(void), and not not getting any values.
Please let me know how i can do this,,
Thanks in advance
8/4/2010 5:00:12 AM
when making remote calls the result handler is not synchronous process, means the code is not executed in one go. The result handler is not guaranteed to be fired. And it can take any amount of time to return data , so there is no way it should return data by just calling function. Only those functions return value which can execute process in one go and are guarantee the data as return without waiting time.
8/4/2010 6:09:15 AM
I can think of a work around for this . you can pass a reference of a listener function to your "remoteHandler" function. This listener function can be anywhere in your application .
say you call the method YourClass.remoteHandler(listenerFunction)
now inside the function use event listener as
ro.addEventListener(ResultEvent.RESULT,listenerFunction);
now when ever the result is returned your listener function will be called.
8/4/2010 7:15:32 AM
@Friend
Thank you.
Yes, i understand your point.
Then is here any better way to get the values from RemoteObject to loop.
I need to create one reusable class or function so that i can fetch remote object mant times. In my case i need to fetch the remote object values from more than 20 java methods
as per the input from flex button.
Thanks in advance
8/4/2010 7:23:52 AM
@Wilbur,
Thank you.
I tried the way you mentioned , But still i couldn't.
Can you please send more specific code with listener function example
8/4/2010 8:44:29 AM
remote functions are not usually used in reusable classes, mostly they are done inline where required. In for loop you can call the remote functions but can never receive the result, for loop does not wait and will execute with in some milliseconds whereas the time of remote call is uncertain.
even if you call remote function 3 time you cannot be sure that you will get the result of first call first and so on in sequence, you can get the result of 3 rd call first and then first then second. You are heading in the wrong direction. To my knowledge the best way around is to declare the remote method and listener where your require them.
using the way suggested by wilbur will only save you from declaring the remoteHandler again and again but you have to declare new listener function for every call you make
8/4/2010 11:08:19 AM
@Friend.
Yes... i faced the problem you have mentioned.
then how i can solve my problem.
I need to create 5-10 containers(eg:Panel) depends upon the rows of data which is in database.
I am fetching it using java.So, we consider it as first level.
Now inside that container, i need to add buttons, and No of buttons
in container depends the rows in database, So again i need to fetch the data,
If i click on any button, it send request to java with some parameter
and display the chart.
Here is the moke-up which i design for you for better understanding.
http://i38.tinypic.com/2l8jdhv.jpg
Can you help me how to do it?
Thanks again...
8/4/2010 12:29:05 PM
call the remote method which return you xml, load xml which tells you the number of containers and the buttons contained inside. Read the xml and create panel and buttons inside. The xml could be like
<root>
<panel>
<button1 text="button1"/>
<button2 text="button2"/>
</panel>
<panel>
<button1 text="button1"/>
</panel>
</root>
Create the buttons with id like panel1Button1 ,panel2button2 so that you can recognize which button is clicked.
add the click listeners to buttons and on click load the related data from remote method to show graph.
on all buttons call a single method
private function clickButton(evt:Event):void{
if(evt.target.id=="panel1Button1")
{
//load data for button 1 in panel1
}else if(evt.target.id=="panel1Button2")
{
//load data for button 2 in panel1
}