You shouldn't call $create from within a component to create another component
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
.component;
Sys$Component$_setReferences(component, components
.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:
- create the component by using new
- set up its properties
- call its initialize method
- add it to the Sys.Application component collection )use the addComponent method).