2 Replies Latest reply on Jan 9, 2007 3:21 PM by FlightGuy

    HELP bind static method in external AS file

    tobydeh
      Hi Guys. I have an external .as file that has an event binded to a static method. I need to call this event somehow but im having trouble as all my methods are static. here is the code:

      Lang.as: (external AS package)


      // UserData ActionScript file
      package
      {
      import mx.resources.ResourceBundle;
      import flash.events.Event;
      import flash.events.EventDispatcher;
      import mx.controls.Alert;
      import mx.binding.utils.ChangeWatcher;
      import mx.rpc.events.ResultEvent;
      import mx.core.Application;
      import mx.events.FlexEvent;

      [Bindable]
      public class Lang extends Application
      {
      static public var lang:String = 'en';

      //english
      [ResourceBundle("en_common")]
      static public var common_en:ResourceBundle;
      [ResourceBundle("en_titles")]
      static public var titles_en:ResourceBundle;
      [ResourceBundle("en_buttons")]
      static public var buttons_en:ResourceBundle;

      //french
      [ResourceBundle("fr_common")]
      static public var common_fr:ResourceBundle;
      [ResourceBundle("fr_titles")]
      static public var titles_fr:ResourceBundle;
      [ResourceBundle("fr_buttons")]
      static public var buttons_fr:ResourceBundle;

      [Bindable(event="langChanged")]
      static public function getline(bundle:String, key:String):String
      {
      return Lang[bundle+'_'+Lang.lang].getString(key);
      }

      [Bindable(event="langChanged")]
      static public function _getline():void
      {
      Alert.show('yes');
      }

      static public function langChange():void
      {
      var e:Event = new Event("langChanged");
      var d:EventDispatcher = new EventDispatcher;
      d.dispatchEvent(e);
      }

      }
      }


      From within my application i then try to call the following:


      Lang.lang = 'fr';
      Lang.langChange();


      Lang.langChange() is called as it should be... and the event is dispatched although the "getline" method is not initiated by the "langChanged" event.

      Please help its been 12 hours of misery so far!!!

      Kind regards,

      Toby.
        • 1. Re: HELP bind static method in external AS file
          tobydeh Level 1
          Community support is lacking big-time. Shame.
          • 2. Re: HELP bind static method in external AS file
            FlightGuy Level 1
            There was all of 40 minutes between your post and your complaint about lack of support. I'm not sure when you last contributed to the 'community' that puts you in a position to make statements like that. You may or may not realize that the contributors to this forum are not paid or employed to sit here waiting for your post so we can all jump on it within 10 minutes and have your problems solved.

            That said...
            You're misunderstanding what Binding is.

            Binding does not identify a method to execute when an event happens - rather, you can set the [Binding] tag before a variable or a get accessor (function get ...). This tells flex to generate code to dispatch the event when that value changes. By marking any property in a class as [Bindable], the class itself becomes an IEventDispatcher. You can also explicitly set it to be such.

            In your example, get rid of the langChange function. I'm not sure you can bind to a static variable - if you have problems, try making it an instance variable - you'll only have one instance of an application anyway.

            Make lang bindable:

            [Bindable]
            public var lang:String = 'en';


            Now add a listener - you can do this in the constructor, or in the creationComplete event - wherever you like.

            addEventListener("propertyChange", handlePropertyChange);

            Define the listener:

            private function handlePropertyChange(event:PropertyChangeEvent):void{
            if (event.property == 'lang'){
            trace('lang changed from ' + event.oldValue + ' to " + event.newValue);
            }
            }

            Then when you want to change language, just do Lang.lang="fr".

            There is no reason in this example to extend Application with your custom class - you could just as well use application, define the properties and listeners in the script block, then when you want to change language:

            Application.application["lang"] = "fr";

            Tim