12-17-2010 03:06 AM
Is it possible to do an async method call ?
What do I mean with that : when a view is loaded I would like to first show the interface to the user and when the data is retrieved from the web server load the data into the view. So the user doesn't has to wait until the data is retreived to see the view. For this first step how do we do that? And as a second step I would like to knwo if it is possible to to an async call to create the view and load the data so that even this could make the app quicker.
Solved! Go to Solution.
12-17-2010 03:30 AM
hey charly,
you should be able to accomplish this using the URLLoader objects load() method. When loading content from a web page, the load method does so asyncronously. Based on the status of the load() method you can check to see if its operation is complete using the Event.COMPLETE event. Like this:
package
{
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.text.TextFieldAutoSize;
import qnx.ui.buttons.LabelButton;
import qnx.ui.text.Label;
[SWF(width="1024", height="600", backgroundColor="#CCCCCC", frameRate="30")]
public class URLLoaderTest extends Sprite
{
private var loader:URLLoader;
private var request:URLRequest;
private var myTextField:Label;
private var myBtn:LabelButton;
public function URLLoaderTest()
{
super();
// support autoOrients
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
request = new URLRequest("http://www.digg.com");
loader = new URLLoader();
loader.load(request);
loader.addEventListener(Event.COMPLETE, handleData);
myTextField = new Label();
myTextField.setSize(100, 50);
myTextField.setPosition(10,10);
myTextField.textField.autoSize = TextFieldAutoSize.LEFT;
myBtn = new LabelButton();
myBtn.label = "This will already be here";
myBtn.setSize(200, 70);
myBtn.setPosition(10, 50);
addChild(myTextField);
addChild(myBtn);
}
private function handleData(e:Event):void
{
myTextField.text = "Complete!";
}
}
}
as you'll see everything loads on to the screen even though the website isnt full loaded via the URLLoader object. After that operation is complete it calls the function handData() and the Label object is populated with the words 'complete'. the same concept will apply to any situation involving the URLLoader.
hope that helps. good luck!
12-17-2010 03:35 AM
Thank you for the information.
12-17-2010 04:24 AM
I was thinking about to implement this with an DAO and MODEL in as.
The MODEL will call the DOA that will do the URLLoader request. So I assume that the DOA will need to notify the MODEL or the View that it is complete?
12-17-2010 04:41 AM
hey charly,
im a little rusty on the whole concept of Model-View-Controller. But i'll give it a shot. since you are loading your data into the model you just need an intermediary to receive a response from the model that the data has been loaded and then have that intermediary notify the view that there was a change and the data is ready. then the view can load it on to the screen to where it needs to be seen. The intermediary can be your controller i presume. Let me know if that made any sense hah.
12-17-2010 05:19 AM - edited 12-17-2010 06:17 AM
What you said makes sence, I use the MVC with asp.net but there Microsoft implemented it direclty and you are not involved in some aspects like the event handling. So I will skip this currently.
Maybe another approach, simpler, is needed.
EDIT:
I just read that as 3 has nu threading support , so I could not do a multi-threading approach.
12-17-2010 06:33 AM
hey charly,
yeah it doesnt have threading (at least not public ones - ive read they do internally in some places) but they do have ASync methods that mimic the approach. The URLLoader class and File class are two examples of some. It's not true multi-threading but it gets the job in most places.
also on a side note there was a thread posted here about an MVC approach that you may or may not be interested in. here's the link:
good luck!
12-17-2010 09:59 AM
There are several MVC frameworks available in AS3 that does a lot of the work for you. I personally avoid them because they enforce one methodology, but for many applications, others swear by them.
12-17-2010 02:29 PM
Hi,
here is a nice implementation that don't use any framework. It's pretty clean and get the job done,that what i use in my projects:
http://unitedmindset.com/jonbcampos/2009/08/18/fle
Hope this helps,
Fabien