I'm trying to create another component in the component's initialize method.
When I create the component in the pageLoad stage, it works fine. But If I create the component in the Init stage, the second component's initialize method doesn't get callled.
this is something that i have never done. generally, i'd create the control outside and pass it as reference to the top control (but again, you might have a requirement which makes this impossible). if you look at the Sys$_Application$endCreateComponents
you'll see why you're not getting the call. here's the code for that 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;
}
when you get the initialization part of the for, components' length is set to 1 since you only have one component. during the 1st step of the cycle, you'll get a new component added to that collection. unfortunately, the above code will always be false and
the initialize of the second component won't be called.
--
Regards,
Luis Abreu
email: labreu_at_gmail.com
EN blog:http://msmvps.com/blogs/luisabreu
Marked as answer by null12345 on Oct 17, 2007 03:01 AM
Luis is right, $create is only intended as a shortcut for page developers and as something that server-side components use to instantiate themselves from in-page code. Component developers writing client-side code should just new up the component and initialize
it "by hand".
$create has this two pass model that hides the complexities of creating components that refer to each other. For code inside a component, there is no need for such a model.
Also, in production code, you'd assign the created component to a member field so that you can properly dispose of it from the dispose of your primary component.
Bertrand
----
This posting is provided "AS IS" with no warranties, and confers no rights.
null12345
Member
12 Points
38 Posts
creating another component in the component's initialize method
Oct 16, 2007 05:08 PM|LINK
I'm trying to create another component in the component's initialize method.
When I create the component in the pageLoad stage, it works fine. But If I create the component in the Init stage, the second component's initialize method doesn't get callled.
Here's the codes.
Type.registerNamespace('Samples'); Samples.MyComponent1 = function() { Samples.MyComponent1.initializeBase(this); } Samples.MyComponent1.prototype = { initialize : function() { Samples.MyComponent1.callBaseMethod(this, 'initialize'); $create(Samples.MyComponent2, {}, {}, {}); alert(Object.getTypeName(this) + " get initialized!"); }, dispose : function() { alert(Object.getTypeName(this) + " get disposed!"); Samples.MyComponent1.callBaseMethod(this, 'dispose'); } } Samples.MyComponent1.registerClass('Samples.MyComponent1', Sys.Component); Samples.MyComponent2 = function() { Samples.MyComponent2.initializeBase(this); } Samples.MyComponent2.prototype = { initialize : function() { Samples.MyComponent2.callBaseMethod(this, 'initialize'); alert(Object.getTypeName(this) + " get initialized!"); }, dispose : function() { alert(Object.getTypeName(this) + " get disposed!"); Samples.MyComponent2.callBaseMethod(this, 'dispose'); } } Samples.MyComponent2.registerClass('Samples.MyComponent2', Sys.Component); if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();Luis Abreu
All-Star
25674 Points
5369 Posts
MVP
Re: creating another component in the component's initialize method
Oct 16, 2007 07:44 PM|LINK
hello.
this is something that i have never done. generally, i'd create the control outside and pass it as reference to the top control (but again, you might have a requirement which makes this impossible). if you look at the Sys$_Application$endCreateComponents you'll see why you're not getting the call. here's the code for that 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;
}
when you get the initialization part of the for, components' length is set to 1 since you only have one component. during the 1st step of the cycle, you'll get a new component added to that collection. unfortunately, the above code will always be false and the initialize of the second component won't be called.
Regards,
Luis Abreu
email: labreu_at_gmail.com
EN blog:http://msmvps.com/blogs/luisabreu
bleroy
All-Star
15617 Points
2302 Posts
Re: creating another component in the component's initialize method
Oct 16, 2007 09:05 PM|LINK
Luis is right, $create is only intended as a shortcut for page developers and as something that server-side components use to instantiate themselves from in-page code. Component developers writing client-side code should just new up the component and initialize it "by hand".
$create has this two pass model that hides the complexities of creating components that refer to each other. For code inside a component, there is no need for such a model.
Also, in production code, you'd assign the created component to a member field so that you can properly dispose of it from the dispose of your primary component.
----
This posting is provided "AS IS" with no warranties, and confers no rights.
Luis Abreu
All-Star
25674 Points
5369 Posts
MVP
Re: creating another component in the component's initialize method
Oct 16, 2007 10:10 PM|LINK
hello again.
btw, i've just written a small post detailing the problem and how to solve it:
http://msmvps.com/blogs/luisabreu/archive/2007/10/16/you-shouldn-t-call-create-from-within-a-component-to-create-another-component.aspx
Regards,
Luis Abreu
email: labreu_at_gmail.com
EN blog:http://msmvps.com/blogs/luisabreu
null12345
Member
12 Points
38 Posts
Re: creating another component in the component's initialize method
Oct 17, 2007 02:59 AM|LINK
Thanks a lot for the detailed explanation!