środa, 5 sierpnia 2009

namespace stuff for flex and as3

Well namespace here namespace there. Let me see what is that namespace in as3 ?

If you were ever programming flex framework you already have used it!

I'll try to show you the whole magic:


xmlns:mx="http://www.adobe.com/2006/mxml"

That's it.

layout="absolute"
minWidth="1024"
minHeight="768">



The namespace is "http://www.adobe.com/2006/mxml" and the namespace accessor is simply "mx". You can change it as you wish so typing



layout="absolute"
minWidth="1024" minHeight="768">



will be ok, but... Follow the white rabbit(The Rules) !

If you change to super and accessor will be mx



layout="absolute"
minWidth="1024"
minHeight="768">



"The prefix "super" for element "super:Application" is not bound."

If you change the namespace to super and live the "mx"



layout="absolute"
minWidth="1024"
minHeight="768">



it will throw you error again "The prefix "mx" for element "mx:Application" is not bound."

Cause it couldn't find the namespace for your prefix in the component definition.

So what when you want expose some of your components. You can add the same namespace but with different prefix ex.



xmlns:super="http://www.adobe.com/2006/mxml"
layout="absolute"
minWidth="1024"
minHeight="768">



remember

you cannot duplicate the namespaces with same accessor

xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute"
minWidth="1024"
minHeight="768">



is wrong cause there cannot be two "mx" accessors


If you ever created custom flex component and you placed it for ex. in my.beutiful.components.*
your namespace will be the path to them and you can name your accessor as you wish - ex. you can type:



xmlns:super="my.beautiful.components.*"
layout="absolute"
minWidth="1024"
minHeight="768">





If you have only one component from my.beautiful.components.* it will be correct to also type:



layout="absolute"
minWidth="1024"
minHeight="768">



So you can refactor your application without remember to clean out your main container namespace.


Well you if you don't follow the rules you can now type:




xmlns:display="flash.display.*"
width="100%" height="100%">



But please don't do this :D


Well what's with as3 ? Why we need namespaces ? Let's see flex again and will create some more magic canvas



package my.beautiful.components
{
import mx.containers.Canvas;

public class SuperCanvas extends Canvas
{
public function SuperCanvas()
{
super();
}
}
}

Simply add some default flex namespace to it called mx_internal



package my.beautiful.components
{
import mx.containers.Canvas;
import mx.core.mx_internal;

use namespace mx_internal

public class SuperCanvas extends Canvas
{
public function SuperCanvas()
{
super();
}
}
}

Now we can access all the mx_internal functions and properties and change them - sometimes it is very handy. Especially when creating custom components.

To check what functions are mx_internal by Ctrl+Space eclipse shortcut you need some Flash Builder beta. Just grab one from adobe labs site
You simply type there override mx_internal function and the shortcut (Ctrl+Space) and here we go - all the mx_internal functions pop up in the window.

We will use some mx_internal stuff now to change something simple:



package my.beautiful.components
{
import mx.containers.Canvas;
import mx.core.ScrollPolicy;
import mx.core.mx_internal;

use namespace mx_internal

public class SuperCanvas extends Canvas
{
public function SuperCanvas()
{
super();
_horizontalScrollPolicy = ScrollPolicy.ON;
_verticalScrollPolicy = ScrollPolicy.OFF;
}
}
}

We set the scroll policy in the canvas directly not trough reference. So to be clear - you use your namespace to cover some of your code, that you don't want the not experienced user to use directly.
Some user with your code can do something like that:



package my.beautiful.components
{
import mx.containers.Canvas;
import mx.controls.Button;
import mx.core.ScrollPolicy;
import mx.core.mx_internal;

use namespace mx_internal

public class SuperCanvas extends Canvas
{
public function SuperCanvas()
{
super();
_horizontalScrollPolicy = ScrollPolicy.ON;
_verticalScrollPolicy = ScrollPolicy.OFF;
}

override public function set horizontalScrollPolicy(value:String) : void {
//
}

override public function get horizontalScrollPolicy() : String {
return ScrollPolicy.AUTO
}
}
}

If you need to use some of the method in other class not by inheritance and don't want to expose it to the world use namespace

keep looking at the Fex SDK source code and learning, I hope that I helped you

Brak komentarzy:

Prześlij komentarz