PDA

View Full Version : Passing arguments to an extended class



DiscoBoy
24 Jun 2009, 5:24 AM
Sorry, after fighting long with it i (extjs beginner) decided to ask again here.

I created an extended class the following way (Saki's 'How to write a big application'):


WS.ui.ProcessModulePanel = Ext.extend(Ext.Panel, {
constructor: function(config) {
// Configuration
Ext.apply(this, {
title: ???arguments???
});
WS.ui.ProcessModulePanel.superclass.constructor.apply(this, arguments);
},
// Init behavior
onRender: function() {
this.createControls(arguments[0]);
WS.ui.ProcessModulePanel.superclass.onRender.apply(this, arguments);
},
createControls: function(processModule) {
alert(processModule);
}
});
Ext.reg('ProcessModulePanel', WS.ui.ProcessModulePanel);in the application.js where i create a new instance of ProcessModulePanel, i have a variable, let's say processName which get from reading in a file.

Now I would like to call ProcessModulePanel and pass the processName as an argument which is then applied as title in the exended panel class. I tried several approaches, but none worked out. 'arguments' in WS.ui.ProcessModulePanel is an object which don't know how to access!



var processpanel = new WS.ui.ProcessModulePanel({processName: 'myProcess'});
var processpanel = new WS.ui.ProcessModulePanel(processName);
etc...Thx for helping me with this basic understanding of ExtJS!

exo
24 Jun 2009, 5:29 AM
WS.ui.ProcessModulePanel = Ext.extend(Ext.Panel, {
constructor: function(config) {
// Configuration
Ext.apply(this, {
title: config.processName
});
WS.ui.ProcessModulePanel.superclass.constructor.apply(this, arguments);
},
// Init behavior
onRender: function() {
this.createControls(arguments[0]);
WS.ui.ProcessModulePanel.superclass.onRender.apply(this, arguments);
},
createControls: function(processModule) {
alert(processModule);
}
});
Ext.reg('ProcessModulePanel', WS.ui.ProcessModulePanel);


This work?

DiscoBoy
24 Jun 2009, 5:47 AM
Hurrrraaaayyyy!
Thank you very very much. I thought this must be very simple but after endless trial&erros i gave up. I was searching for the argument in the argument object most of the time.

One thing i still don't understand is why i can't access global variables in the class itself (and that's why i started to pass arguments):

Example:

var scenario;
var processpanel = new WS.ui.ProcessModulePanel({processName: 'myProcess'});


This way I can't access scenario in procespannel!

pmatsumura
24 Jun 2009, 9:03 AM
Try omitting the "var" like so:


scenario = '';
var processpanel = new WS.ui.ProcessModulePanel({processName: 'myProcess'});

Does this work?
Patrick

DiscoBoy
24 Jun 2009, 11:33 PM
Thx! This works, but why? What is the difference between instantating a variable with var and without?

Condor
24 Jun 2009, 11:36 PM
With 'var' a variable is declared locally within the scope of the function. It can't be accessed outside the function and it will be cleaned up as soon as the function call ends.

Without the 'var' a variable is global (it actually becomes a property of the 'window' objects). So the following statements are equal:

hello = 'world';
alert(hello);

window.hello = 'world';
alert(window.hello);
The first example works because 'window' is the default scope. If you change the scope you can only use the bottom example.

DiscoBoy
25 Jun 2009, 12:05 AM
Somehow i feel ashamed for such beginner questions :)
But that happens when you simply start learning by doing....
Thx for all the fast answers!