Tuesday, May 10, 2011

Why Flex? Examples

In the last post, we just have a feel of why flex is. In this post I would like to support each point we discussed in the last post with examples.

First let us go with programming model of the flex. Flex supports component based programming model as we discussed earlier.
I just want to create a component which can be reused in many applications. Let us start with a simple Hello World component and use it in another application as a component.

Here is the Main.mxml (Which is a application includes HelloWorld.mxml component.

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
   xmlns:s="library://ns.adobe.com/flex/spark"
   xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" xmlns:examples="flex.examples.*">
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<examples:HelloWorld id="helloWorld"  width="100%" height="100%"/>
</s:Application>


Here is the HelloWorld.mxml component.

<?xml version="1.0" encoding="utf-8"?>
<s:BorderContainer xmlns:fx="http://ns.adobe.com/mxml/2009"
   xmlns:s="library://ns.adobe.com/flex/spark"
   xmlns:mx="library://ns.adobe.com/flex/mx" width="400" height="300">
<fx:Script>
<![CDATA[
import mx.collections.ArrayList;
import mx.controls.Alert;

import spark.events.IndexChangeEvent;
[Bindable]
private var list:ArrayList = new ArrayList([{label:"Flash"},{label:"Flex"},{label:"AIR"}, {label:"Adobe"}]);

protected function dropdownlist1_changeHandler(event:IndexChangeEvent):void
{
// TODO Auto-generated method stub
Alert.show("Hello, Welcome to the world of "+combo.selectedItem.label,"Message");
combo.selectedIndex = -1;
}

]]>
</fx:Script>
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<s:DropDownList id="combo" x="171" y="118" dataProvider="{list}" change="dropdownlist1_changeHandler(event)">
</s:DropDownList>
<s:Label x="126" y="123" text="Select" height="15"/>
</s:BorderContainer>

In flex, xmlns tag is used to shorten the package where the component is placed and last part of the package is used as a tag for that component to include in other component or application.

If you observe from the code it is clear that HelloWorld.mxml and Main.mxml are two different components. But I used HelloWorld.mxml inside Main.mxml (Main.mxml is composed from HelloWorld.mxml). This is just a simple example. You can do lot more things with flex components. Check the output of the above code from the below picture.  Selecting one of the items (Flash, Flex, AIR, Adobe) from the list will alert you with the message.




Monday, May 2, 2011

Why Flex?

Everything is very difficult until you start working on it. Same thing happened with me. I'm a Java developer, but the company recruited me asked to work on flex. You don't know what you are going to work on until you are recruited by some company unless you start your own company. It was like a Greek and Latin for me at the initial stage of learning flex. Later on I started liking development in flex.

Okay, lets dig into why flex ?? I'm not gonna discuss why not others(Alternative to flex), becoz I don't know much about them.

Here are the 6 points which make easy to understand why flex ?

1. Programming Model
2. Developer Tools
3. Server Integration
4. Run time Consistency
5. Components and their Customization
6. Performance

1. Programming Model: Developers write Object Oriented code in AS3 (Action Script 3), MXML and CSS (Cascading Style Sheets). These languages can be easily learn by developers who are familiar with Java, PHP, C#, HTML and Java Script. Flex supports component based programming model. What does it mean is that flex applications are assembled from hundreds of components and extend base components.It will be more clear once we try a simple example.


2. Developer Tools: Flex SDK (Software Development Kit) is a free software, which includes compiler and debugger. There is also an IDE (Integrated Development Environment) called Flash Builder which build on top of Eclipse to develop flex applications. It includes design view and code view. It supports code refactoring, smart editing and code completion. Flash Builder includes visual debugger along with some other features (which I haven't used yet).


3. Server Integration: Flex applications run on client either in a browser with flash player or on desktop with Adobe AIR, or on mobile devices. To access back end data, flex supports numerous networking APIs and protocols.

4. Run time Consistency: Flex source code in MXML and in AS is compiled into flash byte code (.swf) which runs on client side by Action Script Virtual Machine in Flash Player or in Adobe AIR. These two are cross platform flash run times that work consistently on variety of hard ware, operating systems and browsers.

5. Components and their Customization: Flex SDK contains hundreds of in-build components, which are the building blocks of flex applications. These components can be styled and skinned to fit the look and feel of the application. Ex: Data Grids, Charts, Formatters etc.

6. Performance: Data Visualization and User Interactions which are done on client side make server response time reduced. This makes back ends more scalable and efficient. Interactions like filtering and sorting don't need to go to Server or Database. This also makes software more responsive and easier to use.