Swiz 1.0 has introduced the ability to create your own custom metadata processors. Although the 1.0 release is fairly new, many custom processors have already been developed by members of the Flex community. I thought I'd take a stab at creating a metadata processor that simplifies adding deep linking support to your application.
The metadata processor I've created is called URLMapping which allows you to easily map URLs to methods. Here's a simple example:
[URLMapping( url="/helloWorld" )]
public function sayHelloWorld():void
{
model.msg = "Hello world!";
}
The URL Mapping processor will automatically start listening for URL changes. For the above example, any time the URL changes to flexapp.html#/helloWorld the sayHelloWorld() method will automatically get called. Also, since url is the default metadata argument, the following example works exactly the same:
[URLMapping( "/helloWorld" )]
public function sayHelloWorld():void
{
model.msg = "Hello world!";
}
You can also use parts of the URL as parameters:
[URLMapping( "/hello/{0}" )]
public function sayHello( name:String ):void
{
model.msg = "Hello " + name + "!";
}
And optionally change the browser window title when the URL changes:
[URLMapping( url="/hello/{0}", title="Hello {0}!" )]
public function sayHello( name:String ):void
{
model.msg = "Hello " + name + "!";
}
Lastly, [URLMapping] also works in reverse with the help of the Swiz [Mediate] metadata. In this example the URL will change to /hello/Ryan and the browser window title to "Hello Ryan!" when the HelloEvent.HELLO event is dispatched:
[URLMapping( url="/hello/{0}", title="Hello {0}!" )]
[Mediate( event="HelloEvent.HELLO", properties="name" )]
public function sayHello( name:String ):void
{
model.msg = "Hello " + name + "!";
}
And then dispatch the event:
<s:TextInput id="nameInput" text="Ryan" />
<s:Button label="Say Hello" click="dispatchEvent( new HelloEvent( HelloEvent.HELLO, nameInput.text ) )" />
[URLMapping] Getting Started Resources
I've created a simple Customer App example, with view source enabled, to show how you could use [URLMapping] in a full application.