2 Replies Latest reply on Mar 16, 2009 8:26 AM by jeffpeck1

    Instances available to multiple components

      I have a custom web service class that is a wrapper for an XMLRPC class that I use to communicate with our web server.

      In every component that uses this class, I end up having to put:
      var client:wsClient = new wsClient();

      For each component this class has to be initialized and given it's own instance.

      It seems like it would make more sense if I could declare that once globally, but Flex does not allow globals to be accessed by all components.

      What is the typically correct way to have a global instance like that?

      Also, I have some constants like UPLOAD_URL that I have redefined in several places for the same reason.

      Your suggestions are appreciated.
        • 1. Re: Instances available to multiple components
          Ben Spink
          Why not make it a static variable...or place these items in your parent Application file. Then they can always be referenced by:

          parentApplication.wsClient.send(parentApplication.UPLOAD_URL); //example

          static public var client:wsClient = new wsClient();
          Or if your app name was "MyApp" using a public static variable would be like:

          MyApp.wsClient.send(MyApp.UPLOAD_URL);// example usign statics.

          • 2. Re: Instances available to multiple components
            jeffpeck1 Level 1
            The problem with the second approach is what if I call:
            parentApplication.wsClient.send(parentApplication.UPLOAD_URL); //example

            before wsClient has finished loading. If I have another component that is going to use wsClient, but the object hasn't been created yet, I would get a runtime error, right? Or would Flex keep things in order to ensure that it is not used by other components until it is first initialized in the main component?

            The first approach of using it as a static variable sounds good, but how would I go about doing that?