inicio mail me! sindicaci;ón

SWFAddress url sorting utility

UPDATE:

I don’t know how I missed this, but since v 2.1 SwfAddress has the getPathNames() method which does exactly what my bit of code does… ah well. Anyway I’ll leave it up as it has the basics of string splitting and other stuff…

Thanks to Cam for pointing this out… and while you’re at it check out his blog, he’s a much better coder than myself (but sucks at the guitar ;) )

Just thought I’d post this up as it might be helpful to someone…

Normally if you’re building a site using SwfAddress (and you should, not having any form of deeplinking in your flash site these days is utterly unforgivable) you could just on the swfAddress change do something like

switch(String(e.value).replace("/", "")){

case "news" :

openNews();

break;

case "shows" :

openShows();

break;

}

But what happens if you want to use subpages (/news/news-item), or subsubpages (/news/news-item/photo)… or however many… well, something like this will help you a lot.

var string:String = "/news/news-item";

function sortDeepLink(_deeplink:String):void{

var urlArray:Array = [];

if(_deeplink.charAt(_deeplink.length - 1) == “/”){

urlArray = _deeplink.substring(1, _deeplink.length - 1).split(”/”);

} else {

urlArray = _deeplink.substring(1, _deeplink.length).split(”/”);

}

switch(urlArray.length){

case 0 :

//Home address

trace(”Home”);

break;

case 1 :

//One tier address - ie. /news/

trace(”URL (one tier): ” + urlArray[0]);

break;

case 2 :

//Two tier array - ie. /news/news-item

trace(”URL (two tier): ” + urlArray[0],urlArray[1]);

break;

default :

//Invalid URL

trace(”URL invalid”);

break;

}

}

sortDeepLink(string);

//

You can extend the switch function to handle as many tiers as you need by just adding more cases - you can also add a 404 function in the default case if you want.

I’d love to know if anyone uses this, so post a comment if you do!

3 Responses to “SWFAddress url sorting utility”

  1. Cameron Yule Says:

    FWIW, if you’re going to be instantiating objects within that case statement it’s probably worth following the factory method pattern; http://en.wikipedia.org/wiki/Factory_method_pattern

  2. Cameron Yule Says:

    Also, I don’t think you have to worry about manually parsing the URL segments into an array. SWFAddress provides the pathNames accessor in the SWFAddressEvent class which does this already.

    http://www.asual.com/swfaddress/docs/en/as/SWFAddressEvent.html#pathNames

  3. Adrian Says:

    Other Solution:
    //—————
    private function onAddressChange(evt:SWFAddressEvent):void
    {
    var branchs:Array = SWFAddress.getPathNames();
    var page:PageTemplate;
    switch (branchs[0])
    {
    case ‘gateway’:
    page = new GatewayPage();
    trace(’\npage:’ + branchs[0] + ‘/’ + branchs[1] + ‘/’ + branchs[2] + ‘/’ + branchs[3] + ‘/’ + branchs[4] + ‘/’ + branchs[5]);
    break;

    case ”:
    case ‘home’:
    page = new HomePage();
    trace(’\npage:’ + branchs[0] + ‘/’ + branchs[1] + ‘/’ + branchs[2] + ‘/’ + branchs[3] + ‘/’ + branchs[4] + ‘/’ + branchs[5]);
    break;

    case ‘about’:
    page = new AboutPage();
    trace(’\npage:’ + branchs[0] + ‘/’ + branchs[1] + ‘/’ + branchs[2] + ‘/’ + branchs[3] + ‘/’ + branchs[4] + ‘/’ + branchs[5]);
    break;

    case ‘lab’:
    page = new LabPage();
    trace(’\npage:’ + branchs[0] + ‘/’ + branchs[1] + ‘/’ + branchs[2] + ‘/’ + branchs[3] + ‘/’ + branchs[4] + ‘/’ + branchs[5]);
    break;

    case ‘portfolio’:
    page = new PortfolioPage();
    trace(’\npage:’ + branchs[0] + ‘/’ + branchs[1] + ‘/’ + branchs[2] + ‘/’ + branchs[3] + ‘/’ + branchs[4] + ‘/’ + branchs[5]);
    break;

    case ‘contact’:
    page = new ContactPage();
    trace(’\npage:’ + branchs[0] + ‘/’ + branchs[1] + ‘/’ + branchs[2] + ‘/’ + branchs[3] + ‘/’ + branchs[4] + ‘/’ + branchs[5]);
    break;

    default:
    trace(’\nerror:URL invalid’);
    break;
    }
    }
    //—————

    Anyway, I liked your solution, simple and elegant.
    =)

Leave a Reply