PDA

View Full Version : instantiate a grid by xtype allows seeing the columns but doesn't work with "new"?



darind
7 Apr 2010, 1:22 PM
I swear I really did read the docs on this! :-) For instance, http://www.extjs.com/learn/Tutorial:Xtype_defined

If I instantiate an object via xtype, then I am able to view the columns object. But if I change the code to be identical except that it instantiates by new Ext.grid.GridPanel, then the columns object seems undefined. Does anyone understand why? I thought the only difference between the two was lazy instantiation vs early.

This works fine when I push the button. I get an alert box showing the columns object.


var myGrid = {
xtype: 'grid',
...
columns: [ {header: "stuff", ... }],
tbar: [{
text: 'button',
handler: function() { alert(myGrid.columns); }
}]


But if I change that code to instantiate this way instead of using xtype, then I get "undefined" on the alert for myGrid.columns.


var myGrid = new Ext.grid.GridPanel({
//No longer use the "xtype: 'grid'," line
...
columns: [ {header: "stuff", ... }],
tbar: [{
text: 'button',
handler: function() { alert(myGrid.columns); } //THIS NOW RETURNS UNDEFINED!
}]


Can anyone explain to me why those two samples don't both work the same?

karieanis
7 Apr 2010, 2:20 PM
In the first example, myGrid is an object literal which defines the configuration options for your grid.

In the second example, myGrid is now an instance of Ext.grid.GridPanel. You should now interact with it using public properties and methods.

Here's an example that will work for both:



tbar: [
{
text: 'button',
handler: function(btn, e)
{
var grid = btn.findParentByType('grid'),
cm = grid.getColumnModel(), columnDef = cm.config;

alert(columnDef);
}
}]