Wednesday, April 6, 2011

Flex: How can I use the @ContextRoot in a Button or LinkButton

I'm trying to create a button that will simply link back to the context root. I noticed flex has a @ContextRoot attribute that appears to work only in certain cases. For example, if I try to use it in the following mxml:

<mx:Button label="Back to Root" click="navigateToURL(new URLRequest(@ContextRoot()), '_parent')"/>

I get the following error: Error: Attributes are not callable.

I can't seem to find this technique explained anywhere, is there another way?

Thanks for the help! Dave

From stackoverflow
  • Well, the cleanest way I found was to use a function in the script block, and not use @ContextRoot like:

    private function goBacktoHompage():void
     {
      baseURL = Application.application.url;
      var tempUrl:String = baseURL.substr(7,baseURL.length);
      var tempUrlArr:Array = tempUrl.split("/");
      var contextRoot:String = tempUrlArr[1];
      var u:URLRequest = new URLRequest("/" + contextRoot);
      navigateToURL(u,"_parent");
     }
    

    I would assume there is an easier way that could use @ContextRoot, so any other answers that don't use relative paths would be welcomed!

    Thanks to these sites for the help:

    http://blog.devsandbox.co.uk/?p=174

    [Adobe help docs on passing arguments]

    -Dave

  • Thanks to http://devgirl.wordpress.com/ for this solution! I think it is better than the Application.application.url solution:

    Use the HTTPService control:

    <mx:HTTPService id="home" url="@ContextRoot()"/> 
    

    And then in Action Script:

     navigateToURL(new URLRequest(home.url),"_parent");
    

    works like a charm!

0 comments:

Post a Comment