views:

481

answers:

3

I am developing a Flex application on top of Mate framework. In this application, I am using a webservice to retrieve data.
As this webservice as not a fix location URL (depending on where customers installed it), I define this URL in a config file. When the Flex application starts, it first reads this config file, then I would like to use the value I found to initialize the webservice.
But currently, I have no idea how to this.

Here is my EventMap.mxml

<EventMap>
<services:Services id="services" />

<EventHandlers type="{FlexEvent.PREINITIALIZE}">        
    <HTTPServiceInvoker instance="{services.configService}">
        <resultHandlers>
            <MethodInvoker generator="{ConfigManager}" method="loadFromXml" arguments="{resultObject}" />
        </resultHandlers>
        <faultHandlers>
            <InlineInvoker method="Alert.show" arguments="ERROR: Unable to load config.xml !" />
        </faultHandlers>            
    </HTTPServiceInvoker>

In this part, the ConfigManager parse the config file and intitialize a bindable property called webServiceWsdl

Here is my Services.mxml

<mx:Object>
<mx:Script>
<![CDATA[
    [Bindable] public var webservice:String;
]]>
</mx:Script>

<mx:HTTPService id="configService" url="config.xml" useProxy="false" />
<mx:WebService id="dataService" wsdl="{webservice}" useProxy="false"/>
</mx:Object>

How can I initialize this webservice property ?

A: 

Create a singleton class to encapsulate your configuration options and bind a property on the singleton instance into your service definition. We do this a fair bit.

[Bindable]
class Config
{
    private static var instance:Config;

    public static function getInstance ():Config {
        if (!instance)
            instance = new Config();
        return instance;
    }

    public var WEBSERVICE:String = "default value";
}

In Services.mxml:

<mx:WebService id="dataService" wsdl="{Config.getInstance().WEBSERVICE}" useProxy="false"/>

Obviously, you need to update your config instance when you load the config from the file.

lach
Sorry for my late answer.As Preston said, I don't see any huge difference between what I did and your solution.Furthermore, I am quite reluctant to use singleton with Flex
GroovyB
Maybe I've misunderstood your question. But you said you had "no idea how to do it" so I assumed that the code you posted was not working. The solution I posted here is one I have used successfully on numerous occasions. Other benefits are that you create a static API describing your configuration variables and you separate your configuration from your event map. That's a better factoring in my book. What have you got against singletons? Cheers.
lach
A: 

I fail to see how this is different from the one in question. One is a bindable String, the other is a Bindable object.

I have found that when (in the above example) the bindable string associated with the wsdl parameter of the web service changes, the web service never updates.

As such, if the value of the string is not correct out of the gate, the web service will blow an error failing to find the wsdl, and will never try again...even when the string changes value.

Preston

Steven Bird
A: 

You can use this WebService.loadWSDL(runtimeWsdl) ;

where runtimeWsdl is a String type variable containing the dynamic wsdl value.

Thanks, Chandra Sekhar.

Chandra Sekhar Samantaray