Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Callback Method Problems

Status
Not open for further replies.

Sameal

Programmer
Aug 6, 2001
142
0
0
US
I am trying to implement a forms like model in flash mx 2004. I have two classes, Container and Component. The Container contains many instances of Component. Each time an instance of Component is instantiated I assign a method pointer to it that points back to a method on Container called "onComplete()". When the Component is finished formatting itself, it calls the method that is assigned to it's "onCompleteCallback" function member. Then inside Container.onComplete I use the keyword 'this' but this refers to the Component object and not the Container. I find this very strange and wonder if this is a bug compared to other OO languages. If it is not how do I reference the Container from within it's own method?

Here is some code to show what I got.

Code:
class Container {
  private var processing:Boolean = false;

  function Container() {}

  function onComplete(object:Component) {
    // Inside here the keyword 'this' refers to the instance
    // of Component rather then the instance of Container.
    // Since I can't use this, what keyword would I use to
    // reference this instance of the Container class?
  }
}

class Component {
  private var onCompleteCallback:Function

  function Component(callback:Function) {
    this.onCompleteCallback = callback;
  }

  function format() {
    this.onCompleteCallback(this);
  }
}

I thought of adding a member to the Component class that points back to the Container and then using that within the onComplete() method of Container, but I believe that this compromises encapsulation of that object because you would have to know about the Component class to understand the Container's reference to this.container.processing rather then this.processing.

Thanks for any help I can get on the subject.
 
I haven't had time to get around to installing 2004 yet (it's sitting in a box on my desk giving me 'come hither' looks) but here's an example of how this was worked around in MX:

Code:
Class = function () {
	this.foo = true;
	this.callback = function() {
		trace(this.foo);
	};
};
class = new Class();
class.callback();
//
stop();

...not sure how they've set up the syntax for Actionscript 2 but can you maybe define the methods within a class using this.callback=function?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top