LA.NET [EN]

Oct 16

After another busy day at work and half an hour swimming, I went to the ASP.NET AJAX forums to chill out befire going back to the IIS 7 online docs. Today I”ve found a cool post ,,)man, you gotta love these forums! 🙂 ) which asked why creating a component through the $create method within the initialize method of another component didn”t fire the initialize even on the created component. After some debugging, it was obvious that the problem happened in this method:

 

function Sys$_Application$endCreateComponents() {
    var components = this._secondPassComponents;
    for (var i = 0, l = components.length; i < l; i++) {
        var component = components[i].component;
        Sys$Component$_setReferences(component, components[i].references);
        component.endUpdate();
    }
    this._secondPassComponents = [];
    this._creatingComponents = false;
}

 

In the shown example, the page had only one component. so,whem the code enters the for, components.length is 1 and even though that value is updated to 2 because the new component was added  during one of the steps performed within the for, thr l value won”t be updated, thus not calling the initialized method of the second component.

Fortunately, Bertrand was around and clarified the whole thing: you should only use $create when you”re creating the component from your page. if you”re creating the inner component, then you”ll really need to:

  1. create the component by using new
  2. set up its properties
  3. call its initialize method
  4. add it to the Sys.Application component collection )use the addComponent method).

3 comments so far

  1. Bertrand Le Roy
    10:51 pm - 10-16-2007

    Also, if you”re creating a component, you should consider yourself responsible for disposing of it. True, if you added the component to the application (which is not an obligation for nested components), the app is going to do the disposing eventually, but as a component developer, how do you know when this is going to happen? So as a responsible component developer, you should dispose of everything you created from your own dispose method.

  2. Luis Abreu
    8:11 am - 10-17-2007

    Hello.

    I agree with you. however, in that case, I think that adding the component to the component collection is really optional, rigth?

  3. Jay
    12:35 am - 9-3-2008

    How can I Re-$create an existing component that has dissapeared after some javascript manipulation of document.body.innerHTML?