So, the problem with your initial code is that the scope of the
function (what `this` refers to) is not your class, but instead is the
scope of the object it's being called on.

So, essentially your code is trying to call `html_data.doSomething()`,
which doesn't exist.  You really want it to be using the scope you
have when you created the callback.

Delegate solves this.  The first argument to Delegate is the scope you
want the function to run with (usually `this`).  The second argument
is the function to run.  In this case Delegate takes care of scope
issue, so that when you call `doSomething()` it's being called with
the correct scope.

The other way to do it (though I prefer Delegate) is this:

public function grabHTML():Void {
          // loads the .html
          var html_data:LoadVars = new LoadVars();
          html_data.doSomethingScope = this;

          // once the .html is loaded…
          html_data.onData = function():Void {
                      // …run "doSomething()"
                      this.doSomethingScope.doSomething();
          };
          html_data.load("http://somesite.ca/some.html";);
};

In this case you're storing the scope you want it to run as in the
html_data object, so when the callback (onData) is called you can
access the correct scope.  Delegate just removes the need for doing
this.

Note that unlike the code above, Delegate will also cause the
`doSomething` method to be passed the same arguments as are passed to
`onData`.

  -Andy

// does something after the .html has been loaded
public function doSomething():Void {
          trace("doing something");
};

On 2/15/07, Omar Fouad <[EMAIL PROTECTED]> wrote:
mmm still don't get the delegate concept....

On 2/15/07, Muzak <[EMAIL PROTECTED]> wrote:
>
> Look into the Delegate class --> mx.utils.Delegate
>
>
> import mx.utils.Delegate;
>
> class YourClass {
>    private var __rawHTML:String;
>
>    public function grabHTML():Void {
>        var html_data:LoadVars = new LoadVars();
>        html_data.onData = Delegate.create(this, this.htmlDataHandler);
>        html_data.load("http://somesite.ca/some.html";);
>    }
>
>    private function htmlDataHandler(rawData:String):Void {
>        this.__rawHTML = rawData;
>        this.doSomething();
>    }
>
>    private function doSomething():Void {
>        // do something with __rawHTML
>    }
> }
>
> regards,
> Muzak
>
> ----- Original Message -----
> From: "Andrew Murphy" <[EMAIL PROTECTED]>
> To: <[email protected]>
> Sent: Thursday, February 15, 2007 2:48 AM
> Subject: [Flashcoders] Problem with cascading functions within a class
>
>
> Im programming a class which includes something like following methods:
>
>
> // grab some .html
>
> public function grabHTML():Void {
>
>            // loads the .html
>
>            var html_data:LoadVars = new LoadVars();
>
>            // once the .html is loaded
>
>            html_data.onData = function():Void {
>
>                        // run doSomething()
>
>                        doSomething();
>
>            };
>
>            html_data.load("http://somesite.ca/some.html";);
>
> };
>
> // does something after the .html has been loaded
>
> public function doSomething():Void {
>
>            trace("doing something");
>
> };
>
>
>
>
>
>
>
>
>
>
>
> Unfortunately the second function doSomething never happens.  Can anyone
> clue me in as to what Im doing wrong..?
>
>
>
> Thank you. :)
>
>
>
> -[a]-
>
>
>
>
> --
> No virus found in this outgoing message.
> Checked by AVG Free Edition.
> Version: 7.5.441 / Virus Database: 268.17.39/686 - Release Date:
> 14/02/2007
> 7:54 AM
>
> _______________________________________________
> [email protected]
> To change your subscription options or search the archive:
> http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
>
> Brought to you by Fig Leaf Software
> Premier Authorized Adobe Consulting and Training
> http://www.figleaf.com
> http://training.figleaf.com
>
>
> _______________________________________________
> [email protected]
> To change your subscription options or search the archive:
> http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
>
> Brought to you by Fig Leaf Software
> Premier Authorized Adobe Consulting and Training
> http://www.figleaf.com
> http://training.figleaf.com
>



--
Omar Fouad - Digital Emotions...
-
Love is always patient and kind. It is never jealous. Love is never boastful
nor conceited It is never rude or selfish. It does not take offense and is
not resentful. Love takes no pleasure in other people's sins...but delights
in the truth. It is always ready to excuse, to trust, to hope... and to
endure... whatever comes.
----------------------------------------

لحب صبور دائماً وهو أبداً ليس غيور
الحبّ أَبَداً ليست متبجّح ولا مغرور
وهو أَبَداً ليس وقح أَو أناني
ولا يعاتب
ولايستاء
لحبُّ لا يسعد
بذنوبِ الناسِ الآخرينِ
لكن المسرّاتَ في الحقيقةِ
هو جاهز دائماً للإعْذار،
للإتِمان، للتَمنّي
وللتَحَمُّل
مَهْما يحدث

_______________________________________________
[email protected]
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com

_______________________________________________
[email protected]
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com

Reply via email to