YUI 3 App Framework ( Y.App, Y.Router, Y.View )

Before we get started I want to make one point clear, I am not saying that I will show you the best way you can  use YUI 3 App Framework, it’s just my view on how you can use it and put it to practice with a simple example. I am pretty sure there are other ways you can use this framework so fell free to ask your friend “Google” and find more information about this topic before starting your project.

Because I am a very pragmatic person and I really  belief  in the power of example I will show you how I use YUI 3 App Framework with a simple example. The scenario will be pretty simple:

– you can login
– add users
– see the list of users
– delete a user

Views ( Y.View, Y.Phi.BaseView, Y.Phi.ComposeView )

So the first thing that we will prepare are the views we will use in our application. We have:

1. LoginView
2. MainView
2.1. CreateUserView
2.2. ListUsersView

in total we will have 4 views. A view extends from Y.Phi.BaseView or Y.Phi.ComposeView and has two files:

– View.js
– View.tpl

for example the LoginView.js is a simple view and looks like this:

and the Login.tpl file:

You will see that all my views extends Y.Phi.BaseView. This class extends from Y.View and only adds the ability to load a .tpl file and assign is content to “this.template“. I didn’t like the idea of  writing my template in the js file using quotes so after some search on the internet I have fond this solution of loading the template using an XHR request. I don’t remember the website were I have found this but I think is the most elegant solution.

So to use this you need 2 modules:

– phi-baseview
– template-loader


After we have our views we need to see what kind of data we will use in our application. For this example all we need is:

– Y.User
– Y.ModelList({model: Y.User}) a collection that will contain all our users

And then all we need to do is glue all this using Y.App.


The application code is very basic, I just register the two views ( LoginView and MainView ) and switch between the two views.

Because MainView is a compose view when we will show MainView the CreateUserView and ListUsersView will be displayed too. Another thing that you will see in this example is how to communicate between views. For example when you create a new user using CreateUserView you will want the ListUsersView to redraw the list of the users so you can see the new created user in the list. This will be easy to achieve. In ListUsersView we will add:

The routes are self explanatory:

And this is all. To learn more please study the code and read the comments.

Demo application
Download the source ( 40.227 Kb )

Leave a comment if you have any questions.

Code formatting for Adobe Flex – Flex Formatter

After using Flash Builder for a log time I think everyone has a lot of custom settings for the workspace they use so whenever you put a fresh OS or just reinstall Flash Builder first think you do is setting up the workspace and I think everyone has his own special thinks to setup. Me for example I install subclipse, setup some custom key shorcuts, edit some perspectives and much more but I think the most useful plugin and a plugin that all of my colleagues use is Flex Formatter, this plugin it’s a must and I install this all the time, again and again and they do the same.

So because of this, most of the time people ask me: From where to download Flex Formatter ? How do I install Flex Formatter ? Can I share my format settings ? so I decided to write the answer to all this questions in a blog article.

From where to download Flex Formatter ?

Answer: You can download a .zip of Flex Formatter from : http://sourceforge.net/projects/flexformatter/

How do I install Flex Formatter ?

Answer: After you have download the .zip  archive all you have to do is to copy all the three .jar files from the archive in the Adobe Flash Builder plugins directory. For example in my case I had to copy the three files here “C:\Program Files\Adobe\Adobe Flash Builder 4\plugins\“.

Can I share my format settings ?

Answer: Yes
The formatting settings I use is :  format.properties

Using AMF and RemoteObject without services-config.xml

This article will be very short but I hope it will be very useful. The goal of this article is to show you how to use a RemoteObject without service-config.xml. The example in this post will be very simple, we will connect to a AMFPHP service and get a hello message from it.

First let’s see the service:

// hello.php
class Hello
	public function sayHello()
		return "Hello back!";

Now in Flex all we have to do is to load a XML file ( channel.xml ) and read the channel from it:

protected function addedToStageHandler( event :Event ) :void
	var loader :URLLoader = new URLLoader();
	loader.addEventListener( Event.COMPLETE, completeHandler );
	loader.addEventListener( IOErrorEvent.IO_ERROR, ioErrorHandler );
	loader.load( new URLRequest( "channel.xml" ));
protected function completeHandler( event :Event ) :void
	// Load info from XML
	var xml :XML = new XML( event.target.data );
	var amfEndpoint :String = xml..channel.( @id == "my-amfphp" ).@endpoint;
	// Create a AMF channel
	var channel :AMFChannel = new AMFChannel( "my-amf", amfEndpoint );
	// Add channel to our RemoteObject
	rmtObj.channelSet = new ChannelSet();
	rmtObj.channelSet.addChannel( channel );

Now if you use the loaded channel for all your RemoteObjects you don’t have to compile again your application if you decide to change your AMF endpoint, all you have to do is to edit the channel.xml file.

This is very useful when you upload your application to a server, you don’t have to compile again the application to work on different servers all you have to do is to use different channel.xml.

You can download the code here ( click to download ).

And this is all. If you have any questions post them here.