Create local reference to current class

Sometimes scope creep is a big pain in the *** when reaching the end in a particular project, but some people also have problems in understanding the scope in external class files. Let’s have a look to an example: Imagine we have a shoppingCart application. We get our information from a MySQL database through PHP. PHP returns to Flash a XML file and when de XML is loaded we want to provide de data to ShopCollection class.This is the main class file:

class ShopEngine
 private var collection : ShopCollection;
 private var container : MovieClip;

 public function ShopEngine (container : MovieClip)
  this.collection = new ShopCollection (container);
  this.container = container;

 public function getXML (path : String)
  //Reference to the current class
  var thisObj : ShopEngine = this;

  var data_xml : XML = new XML ();
  var result_lv : LoadVars = new LoadVars ();
  data_xml.ignoreWhite = true;
  data_xml.onLoad = function (ok : Boolean)
   if (ok)
    thisObj.collection.dataProvider = this.firstChild;
  result_lv.sendAndLoad (path, data_xml, "POST");

As you see, we need a manner to reference the collection property in the onLoad event off the xml object. In the onLoad event, the collection property off the ShopEngine class is not known. When you provide the getXML methode with a variable thisObj you can reference the collection property off the class without problems. You can also reference a particular method off the class.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s