-
3 Jul 2010 8:55 AM #1
when to use initcomponent and when constructor?
when to use initcomponent and when constructor?
I had a problem recently while using initcomponent:
using constructor solved the problem:Code:Ext.ns("APP.view"); APP.view.North = Ext.extend(Ext.Toolbar, { initComponent : function() { Ext.apply( this, { region : 'north', height : 30, // give north region a height border : false, margins : '0 5 0 5', items : this.buildTbar() }); APP.panel.North.superclass.initComponent.call(this); }, .....
My question:Code:Ext.ns("APP.view"); APP.view.North = Ext.extend(Ext.Toolbar, { constructor : function(config) { // config = config || {}; Ext.applyIf( config, { region : 'north', height : 30, // give north region a height border : false, margins : '0 5 0 5', items : this.buildTbar() }); APP.view.North.superclass.constructor.call(this, config); }, .....
Are there situations where it is preferable to use initcompontent? Or is it save to say that constructor is a more safe option in any case?Kind Regards,
Willy. (
Averell Dalton)
-
3 Jul 2010 9:21 AM #2
Because the instances of configs being accessed at construction time, or configs access from initialConfig are not documented, I would say always write your own constructor.
Copy configs into a new object, and fiddle that before calling the superclass constructor.Search the forum: http://www.google.com/coop/cse?cx=01...%3Az7of1ufqccu
Read the docs too: http://extjs.com/deploy/dev/docs/
Scope: http://extjs.com/forum/showthread.ph...642#post257642
-
3 Jul 2010 9:41 AM #3
I just asked that question on stackoverflow.
I wonder why so many examples and books use initComponent. Does it have advantages over constructor?
I think wceuppens did that, or do you mean something differently? It is similar to the guidelines in the wiki.Copy configs into a new object, and fiddle that before calling the superclass constructor.
-
3 Jul 2010 9:47 AM #4
No, wceuppens mutates the config object in his constructor. You have to be careful.
Search the forum: http://www.google.com/coop/cse?cx=01...%3Az7of1ufqccu
Read the docs too: http://extjs.com/deploy/dev/docs/
Scope: http://extjs.com/forum/showthread.ph...642#post257642
-
3 Jul 2010 9:49 AM #5
The code must be
You copy config INTO a brand new object, and then pass that NEW OBJECT into the superclass constructor for it to do its business with.Code:config = Ext.apply({ foo: 'default foo value', ... } config);Search the forum: http://www.google.com/coop/cse?cx=01...%3Az7of1ufqccu
Read the docs too: http://extjs.com/deploy/dev/docs/
Scope: http://extjs.com/forum/showthread.ph...642#post257642
-
3 Jul 2010 10:02 AM #6
If the passed config has references itself, you are still not safe, are you? For example
I see no cloning in Ext.apply:Code:var ref = new Array(1,2,3) var config = {myRef: ref};
PHP Code:Ext.apply = function(o, c, defaults){
// no "this" reference for friendly out of scope calls
if(defaults){
Ext.apply(o, defaults);
}
if(o && c && typeof c == 'object'){
for(var p in c){
o[p] = c[p];
}
}
return o;
};
Otherwise, we need to update the wiki I think?
-
3 Jul 2010 10:38 AM #7
Your code creates a new object, and assigns config to reference it. It's fine.
Search the forum: http://www.google.com/coop/cse?cx=01...%3Az7of1ufqccu
Read the docs too: http://extjs.com/deploy/dev/docs/
Scope: http://extjs.com/forum/showthread.ph...642#post257642
-
4 Jul 2010 3:36 AM #8
Did I miss what the original posters problem was?
I use initComponent over constructor most of the time and can't say I have run into any problems yet.-Shea
My Blog:VinylFox | Twitter:@VinylFox | JavaScript Magazine:JSMag | Curator of the Baltimore/DC JavaScript Meetup | Author: Learning ExtJS 3.x Book
ExtJS Extensions & Plugins: GMapPanel UX | HtmlEditor Buttons Plugin | Selection Enabler Plugin | Grid DataDrop Plugin | Additional Ext.Fx
Sencha Touch Plugins: Swipe Tabs | List Pull Refresh | Accelerometer Tabs
-
4 Jul 2010 3:53 AM #9
But you probably set initialConfig properties?
Search the forum: http://www.google.com/coop/cse?cx=01...%3Az7of1ufqccu
Read the docs too: http://extjs.com/deploy/dev/docs/
Scope: http://extjs.com/forum/showthread.ph...642#post257642
-
4 Jul 2010 6:49 PM #10
I guess I must be if im not running into problems.
Honestly the OP's code seems silly to me, why would you hard code the region of the component your creating, which is where he must have run into trouble since that is set in the constructor. All of the other options he set are trivial.-Shea
My Blog:VinylFox | Twitter:@VinylFox | JavaScript Magazine:JSMag | Curator of the Baltimore/DC JavaScript Meetup | Author: Learning ExtJS 3.x Book
ExtJS Extensions & Plugins: GMapPanel UX | HtmlEditor Buttons Plugin | Selection Enabler Plugin | Grid DataDrop Plugin | Additional Ext.Fx
Sencha Touch Plugins: Swipe Tabs | List Pull Refresh | Accelerometer Tabs
Similar Threads
-
[FIXED-1090]HTMLEditor initComponent does not call superclass initComponent
By mattgoldspink in forum Ext 3.x: BugsReplies: 0Last Post: 1 Jul 2010, 4:50 AM -
constructor & initComponent usecases
By nickar in forum Ext 2.x: Help & DiscussionReplies: 3Last Post: 6 Jul 2009, 1:07 PM -
initComponent or constructor
By nomack84 in forum Ext 2.x: Help & DiscussionReplies: 3Last Post: 3 Apr 2009, 11:02 AM -
constructor Vs initComponent
By santosh.rajan in forum Ext 2.x: Help & DiscussionReplies: 27Last Post: 16 Sep 2008, 5:45 AM -
Confused about using initComponent or constructor
By michael.piecko in forum Ext 2.x: Help & DiscussionReplies: 4Last Post: 20 Apr 2008, 12:36 AM


Reply With Quote
