Welcome, Guest
  • Author Topic: EventDispatcher  (Read 1121 times)

    michael

    • Jr. Programmer
    • **
    • Posts: 71
      • View Profile
      • Email
    EventDispatcher
    « on: 11/25/04, 21:01 »
    How do I implement the EventDispatcher in a regular old class file? I can't seem to get it to work. Here is what I have:


    Class File that I'm trying to get to implement EventDispatching as lightweight as possible.
    /* imports */
    import mx.events.EventDispatcher;

    /**
    * DatabaseConnection class is used as a wrapper for
    * php script invoking.
    */

    [Event("connect")]
    class DatabaseConnection{ // extends EventDispatcher{

       private var host:String;
       private var port:String;
       private var username:String;
       private var password:String;
       private var phpSendVariables:LoadVars;
       private var phpReturnVariables:LoadVars;
       
       /**
        * Constructor to create a database connection
        * @param host  String of the database host
        * @param port  String of the database port
        * @param user   String of the username for the database
        * @param pass   String of the password for the database
        */
       function DatabaseConnection(host:String,port:String,user:String,pass:String){
          EventDispatcher.initialize(this);
          
          this.host = host;
          this.port = port;
          this.username = user;
          this.password = pass;

          phpSendVariables = new LoadVars();
          phpReturnVariables = new LoadVars();
          phpReturnVariables.onLoad = onLoad;
       }
       
       /**
        * Connect to a specific php script
        * @param script String of the script to connect to
        */
       function connect(script:String){
          phpSendVariables.host = this.host;
          phpSendVariables.port = this.port;
          phpSendVariables.username = this.username;
          phpSendVariables.password = this.password;
          phpSendVariables.sendAndLoad(script,phpReturnVariables,"POST");
       }
       
       function onLoad(success){
          trace("Loaded: "+success);
          trace(this["result"]);
          dispatchEvent({type:"connect",target:this});
       }
    }

    Here is my fla code where I am trying to use the above class:
    var dbconn = new DatabaseConnection("www.testserver.com","3600","username","password");

    var l = new Object();
    l.addEventListener("connect",dbconn);
    l.connect = function(obj){
       trace("BOOOOOB");
    }

    dbconn.connect("http://www.shortstopfoodmarts.com/reports/php/test.php");


    Jorge Solis

    • Global Moderator
    • Systems Administrator
    • *****
    • Posts: 14616
      • View Profile
    Re:EventDispatcher
    « Reply #1 on: 11/26/04, 12:18 »
    You need to declare dispatchEvent classes as class functions

    function dispatchEvent() {};
    function addEventListener() {};
    function removeEventListener() {};   

    Check http://www.macromedia.com/devnet/mx/flash/articles/creating_events.html

    Jorge