Posts Tagged ‘AS3’

Application Domains in Flex

Wednesday, March 4th, 2009

In this post I will try to explain the best I can why you need to know about application domains and how to use them. An application domain is the partition within which an application runs in Flash Player.  If you read the documentation you will see that application domain is a container for discrete groups of class definitions.

But what exactly an application domain does and when the understanding of application domain will helps you ? Usually there is just one application running which  implies the existence of only one application domain. Hance you will not notice or care about the application domain. However, when you load additional .swf files into your application you can create additional application domains for all the .swf files you load.

So let’s start. I will try to explain the application domain with an example. Let’s say we have an application named MyApplication that has a class HelloW with one function sayHello() and we want to load two .swf files in this application that has a class with the same name HelloW with one function sayHello() . When we load a .swf file, three possible things can occur:

1. The loaded .swf runs in an existing application domain.
2. The loaded .swf runs in a new application domain that is a child of an existing application domain.
3. The loaded .swf runs in a new application domain that is completely partitioned from all other application domains.

So let’s see for our example what will happen in each of the three cases :

1. The loaded .swf runs in an existing application domain

sameappdomain
Fig 1.0

You can load a swf in the existing application domain like this:

var context :LoaderContext = new LoaderContext( );
context.applicationDomain = ApplicationDomain.currentDomain;

var request  :URLRequest = new URLRequest(”SWF1.swf”);
var loader :Loader = new Loader( );

loader.load(request, context);

In our example, if we load our two swf files in the existing application domain the HelloW class from the first swf and HelloW class form the second swf will be lost against the HelloW class from the main application. Another problem will be that the SWFHelloW class from the second swf will be lost against the  SWFHelloW class from the first swf.  So, in the end, we will have :

- the HelloW class from MyApplication
- the SWFHelloW class from SWF1

Using this application domain we share code and use less memory, but in first SWF1 we have lost the HelloW class this can cause some big errors. All objects from SWF1 that use HelloW will now use HelloW from MyApplication and this class has different functionalities so this will cause some pretty nice errors. In the second SWF2 we have lost the HelloW and the SWFHelloW class. Which  can cause considerable  errors. In the end we will have an app with a lot of errors.

In order for this application domain  to work we must edit our example like this :

- the HelloW class from SWF1 and SWF1 must do the same things as HelloW class from MyApplication
- the SWFHelloW class from SWF2 must do the same things as SWFHelloW class from SWF1.

2. New application domain that is a child of an existing application domain

Neither of this  application domains is  good(suited) for our exemple. But let me show you what is the main difference between this application domain and the one before.

childappdomain1
Fig 2.0

In this application domain we lose the HelloW class but we don’t lose the SWFHelloW class from the second SWF.  We  achieve this application domain like this :

var context :LoaderContext = new LoaderContext( );
context.applicationDomain = new ApplicationDomain(ApplicationDomain.currentDomain);

var request  :URLRequest = new URLRequest(”SWF1.swf”);
var loader :Loader = new Loader( );

loader.load(request, context);

3.  New application domain

This is the solution for our example. Using this application domain in the end we will have:

- the HelloW class from MyApplication
- the HelloW class from SWF1 and SWF2
- the SWFHelloW class from SWF1 and SWF2

newappdomain
Fig 3.0

However, it is important to use these sorts of exclusive application domains only when necessary because they will increase memory usage. I hope this example has shown the importance of application domain and has given you a better understanding on what application domain you need depending on the result you want.

AS3FlexDB

Friday, January 16th, 2009

I think it’s time to write a quick and short  post about my flex library. I will only write a short description and some simple usage example. For more explicit tutorials see iTutorials or Sephiroth.

What is AS3FlexDB ?

AS3FlexDB is an open source library that allows Adobe Flex applications to connect to a MySQL server. This library use AMFPHP to acess a MySQL server.

Why AS3FlexDB ?

So before I start describe the lib let me tell you how this project got live. The ideea of this project was a simple question “Can I have all my SQL’s in AS3 and not in PHP?” how useful can that be !  To be more specific what I was wishing was a library that allow me to manipulate a MySQL Database in AS3 no need for me to write any PHP code or other server side code. So this is why? i have chose to write this library.

How AS3FlexDB execute an SQL Statement ?

So what AS3FlexDB do in background ? The flow of events from your code to MySQL is very simple. AS3FlexDB use “RemoteObject” class to access a AMFPHP service. The AMFPHP service more exactly the php “Database” class execute the SQL and return the results to Flex.

Take a look at next diagram :

As3FlexDB Arhitecture

Now you know what is AS3FlexDB, why I have develop AS3FlexDB and how it works the next important question you must put yourself is “ How can I install and use AS3FlexDB ? “

Install AS3FlexDB

To install AS3FlexDB all you have to do is to install AMFPHP (look here for a tutorial if you don’t know how) copy the file database.php (you can find this file in AS3FlexDB Kit) in your AMFPHP service directory and import in Flex as3flexdb_version.swc library and you are done.

Use AS3FlexDB

You can use AS3FlexDB entirely from MXML or from actionscript.  So first let’s try SELECT all users from a database with MXML. The code for this is :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<mx:Application
	layout="vertical"
	xmlns:phi="phi.db.*"
	xmlns:mx="http://www.adobe.com/2006/mxml">
 
	<mx:DataGrid id="dg1" width="100%" height="100%" dataProvider="{q1.Records}">
		<mx:columns>
			<mx:DataGridColumn dataField="fname" headerText="First Name"/>
			<mx:DataGridColumn dataField="lname" headerText="Last Name"/>
			<mx:DataGridColumn dataField="password" headerText="Password"/>
		</mx:columns>
	</mx:DataGrid>
 
 
 
	<phi:ConnectionData id="c1" name="mxml_conn1"
						host="localhost"
						db="test"
						username="root"
						password="root" />
 
	<phi:Database id="db1" connection="{c1}" />
	<phi:Query id="q1" database="{db1}" q="SELECT * FROM users WHERE 1" />
	<phi:QueryExecute id="q1ex" query="{q1}" />
 
</mx:Application>

If you want to select all users from database using AS3 the code for that will look like this :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/** Database */
import phi.interfaces.IQuery;
import phi.interfaces.IDatabase;
import phi.db.Database;
import phi.db.Query;
/** */
 
private var db       :IDatabase;
private var query    :IQuery;
 
private function onCreateComplete():void
{
	db = Database.getInstance();
	query = new Query();
 
	db.connect("conn1", "root", "", "localhost", "flexdb", true);
	query.connect("conn1", db);}
 
private function selectUsers():void
{
	query.addEventListener(Query.QUERY_END, queryEnd);
	query.execute("SELECT * FROM users WHERE 1");
}
 
private function queryEnd(evt:Object):void
{
	users = query.Records();
}

I think now you have a very solid ideea about what this library do. So if you think it’s what you need or you want to read more documentations the next links can help you.

Documentation | Download

If you want to join the project visit project home page here.
Until next post I will close this post with one of my favorite quote:

“Talent does what it can, genius does what it must.”