0 Replies Latest reply on May 21, 2007 3:39 PM by sneakyimp

    Event Handlers and Callbacks:  Best way to handle?

    sneakyimp Level 2
      I'm writing an actionscript class. I'm wondering how to deal with call backs and event handlers within my class. In particular, I'm wondering how I might structure my class so that developers using it can choose which arguments get passed to the callback function as they code. I've been googling and haven't found any particularly useful information on this.

      As far as I can tell, it would appear that the parameters passed to any callback function are determined at the time I write my class as this anonymous function example illustrates.
      var myObj:Object = new MyClass('foo', 'bar');
      myObj.onLoad = function(arg) {
      // the args passed to this anonymous function are dictated by the actionscript defnining MyClass

      I have also seen an approach using named functions and a class method for setting the event handler which allows a developer to pass some object to be used for scoping the named function but this also results in the parameters for the callback being predetermined by the actionscript that defines the class
      function myLoad(arg) {
      // the args passed to this function are also dictated by the actionscript defining MyClass
      var myObj:Object = new MyClass('foo', 'bar');
      myObj.setOnLoadHandler(this, 'myLoad');

      As far as I can tell, neither approach would let a developer specify any parameters for the callback functions (onLoad() in the first example, myLoad() in the second example). Which of these approaches is considered 'best practice'? Also, what would I do if I wanted to specify that the callback should operate on a particular movieclip on my timeline? How can I pass that movieclip's parameter to the callback function?