If you are planning to build a RIA both in web and desktop format Flex is a good candidate. I suspect that both will be pretty similar, and probably the desktop version will have extended functionalities that exploit the capabilities of the operative system. If you are the programmer of such a RIA you probably might not want to have two separate projects. Or at least you might need to have some core code, shared by projects, which you change just once. Although some solution has already been proposed, here is my take, a very simple one: a symbolic link!
Just put the core code in a Flex project and then create a folder in the Air project, which is not a real folder but a symbolic link to the folder already contained in the Flex project.

Step by Step

Let’s start with a Flex project. We’ll name it MyFlexProject. Then we create a class, called MyClass, as in the picture below.
Creation of MyClass

We will add a trace statement to show when an instance of MyClass is created.

1
2
3
4
5
6
7
8
9
10
11
12
package com.studiomagnolia.shared
{
	public class MyClass extends Object
	{
		public function MyClass()
		{
			super();
			trace("Created an instance of MyClass");
		}
 
	}
}



Now we create the Air project. Let’s call it MyAirProject. And here comes the trick. Open a shell and move to the src directory of the Air project. Here you have to create a symbolic link which points to the src/com folder of MyFlexProject.
The code for bash shell is the following:


      ln -s ../../MyFlexProject/src/com

Now if you refresh MyAirProject you will see that the src folder contains a com subfolder. Cool uh? Now let’s write some code to see whether we can use MyClass in the Air project.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="onCreationComplete()">
 
	<mx:Script>
		<![CDATA[
 
			import com.studiomagnolia.shared.MyClass;
 
			private function onCreationComplete():void {
				var c:MyClass = new MyClass();
 
			}
 
		]]>
	</mx:Script>
 
</mx:WindowedApplication>

In the console you should see the trace statement. You can access/edit/save the core code from both the Flex and the Air project. Try changing the trace statement within the Air project and you’ll see changes reflected into MyFlexProject.

Hope this helps.

Similar Posts

  • Share/Bookmark

Leave a Reply