When I'm developing apps that use remote services, I sometimes start by connecting to a local web server on my own machine. When it's time to take the app live I switch it over to use the public version of the services.
If your app goes against more than one service it can be a real pain to edit all the URLs when you redirect from a local server to a public one and back again. It would be nice to change the first part of the URLs in one place and apply the change to all the URLs in your application at once. Luckily Flex 2 data binding makes this kind of thing almost trivial to implelement.
First, define a bindable variable to hold the changeable URL prefix:
<mx:Script>
<![CDATA[
[Bindable]
public var urlPrefix:String = "http://localhost/"
]]>
</mx:Script>
Next change the url
or wsdl
attributes of your services. Bind the first part of those attributes to the urlPrefix
variable, like this:
<mx:WebService id="MyWebService" wsdl="{this.urlPrefix}/MyWebService.asmx?WSDL">
Or for an HTTP service:
<mx:HTTPService id="MyDynamicWebPage" url="{this.urlPrefix}/MyDynamicWebPage.html">
By default your services will now look for the services in the localhost domain. When you are ready to switch to a production server just change the initial value of the urlPrefix
variable and rebuild your app.
Better yet you can read a urlPrefix
value from a configuration file at runtime, so you can change servers whenever you want without rebuilding your app. This urlPrefix binding technique even lets you switch among mutliple domains at runtime if you have such a need.
"Data binding: it's not just for UI components anymore."