views:

406

answers:

1

Check out this code:

import mx.core.View;
var accordianPane = my_acc.createSegment("mcElectrical", "panel0", "Electrical", "payIcon");
accordianPane.comboBox.addItem("test");

This adds an item with label "test" to a combo box in a movie clip. It works perfectly fine. However, when I put the same code in a callback function, it fails.

import mx.core.View;

// Load Cost Guide feed.
var costGuideUrl:String = "http://test/cost-guide.ashx";
var costGuideXml:XML = new XML();
costGuideXml.onLoad = function(success) {
    if(success) { 
     populateAccordian(this); 
    } else {
     // Display error message.
    }
};
costGuideXml.load(costGuideUrl);

// Populate Accordian with retrieved XML.
function populateAccordian(costGuideXml:XML) {

    var accordianPane = my_acc.createSegment("mcElectrical", "panel0", "Electrical", "payIcon");
    accordianPane.comboBox.addItem("test");
    // This line definitely executes...
}

Any ideas on why I can't get to the combobox from inside the callback?

+1  A: 

Ok so first things first it looks like yo are using AS2.

As it is as2, the problem is probably a scoping issue. Scoping works differently in as2 to as3. Pushing my mind back to my as2 days, when you set this callback function, you are then in the scope of costGuideXML. As you are in this scope you do not have access to the my_acc variable.

What you probably need to use is the Delegate class to make the populateAccordian method execute in the scope of the original class (chances are a movieclip if this is on a timeline).

Something like (although this is untested):

import mx.utils.Delegate;

    // Load Cost Guide feed.
    var costGuideUrl:String = "http://test/cost-guide.ashx";
    var costGuideXml:XML = new XML();
    costGuideXml.onLoad = Delegate.create(this, xmlLoadedHandler);
    costGuideXml.load(costGuideUrl);

    function xmlLoadedHandler() : Void
    {
     populateAccordian(costGuideXml); 
    }

    // Populate Accordian with retrieved XML.
    function populateAccordian(costGuideXml:XML) {

        var accordianPane = my_acc.createSegment("mcElectrical", "panel0", "Electrical", "payIcon");
        accordianPane.comboBox.addItem("test");
        // This line definitely executes...
    }
James Hay