this is a follow-up to the following posts:
In this post, I will add code to identify each request uniquely, give you a class called “AbstractClient” that will handle most of the mechanics and is easily extended. The idea is to give you a base on which to build. Essentially, for every service call you will want to make you will need 2 functions:
For those who’d like to go ahead and enjoy the goods right away, I will write this tutorial so it can be read and understood on it’s own.
Class Member Variables
AbstractClient will contain 2 variables:
protected var _requestQueue:Array;
protected var _requestSequence:Number = 0;
So here is what it looks like so far:
So the queue is initialized to an empty array, and the sequence is set to 0.
The core functionality
So the idea is to keep currently occuring requests in the queue, and to describe the request using a wrapper object. This object will contain:
The wrapper object is passed as a parameter to the public function being called from the outside. We then add information into it and put it in the _requestQueue.
When the loader (URLLoader) receives the result, we get the proper wrapper object from the queue, and then use this information as necessary.
Here is the main function, called runLoader.
So basically the loader is created, we set the right format for the data to be sent and received, listen to all potential events. The Event.COMPLETE event handler is specified by the subclass calling this function. Then assign the wrapper properties and push it in the queue, and return the id of this request to the subclass’ function.
What’s left is the code to handle errors. This is, however, not fully implemented yet. First lets get a few function to retrieve the right Wrapper objects from the queue..
Next the actual error handling functions, which you will have to implement yourself..
You could handle progress from a request, although I doubt that in most cases the “bytesTotal” will be know. Here is some sample code:
AbstractClient Complete Source
here is the source, complete..
So as per Part 2, we woud like it to be a singleton. So here is the start..
Now, here is where the action happens..
This function is public, so it can be called by the object using this WSClient. It returns a number, the unique identifier of the request. First we create the URLRequest object pointing the url of your service. Then we assign GET method to it. Then we put all the variables inside of a URLVariables object, and assign it to the request. Then create the wrapper object, giving it certain params that could be useful later and call runLoader which does the magic for us.
You will notice that runLoader receives a reference to handleProductSizes which is actually a function that we need to define like so:
As a bonus, here is the code for the ProductSizeEvent
Hope this helps! full source code… well you can ask; but this is straightforward, I encourage READING and TRYING and getting it to work. This is invaluable experience and cannot be bought or copy-pasted.
Just want to say thanks for the excellent tutorials. This is incredibly helpful stuff.