PDA

View Full Version : Problem with class constructor



schuc
9 Oct 2012, 11:47 AM
Hi everybody!

Iam developing with Ext js 4 and quite a newbie.
I've build a MVC App and try to redesign and pull out as much as code as possible in structured classes.
My Problem:
I've created a class as followed:

Ext.define('ServiceRequest.view.FacilityTreePanel',{
extend: 'Ext.tree.Panel',
config:{
treePanelId: '',
treePanelTitle: '',
treeStore: ''
},

constructor: function(config){
this.initConfig(config);
return this;
},

title: this.treePanelTitle,
itemId: this.treePanelId,
iconCls: 'nav',
store: this.treeStore
})

As you see i've had an constructor in wich i want to set the title, itemId and store
of the TreePanel.
I'd like to create this class in a Ext.each loop a couple of times.
I've checked that the given parameters to the class constructor are never the same.
For the first time the class is created.
After the second loop i get an

Uncaught TypeError: Cannot read property 'added' of undefined.
It seems like that the constructor call is after the setting of title,itemID and store.
What am i doing wrong?
Thanks a lot for any suggestion.

cheers Thomas

vietits
9 Oct 2012, 5:25 PM
Try to fix your code as below:


Ext.define('ServiceRequest.view.FacilityTreePanel',{
extend: 'Ext.tree.Panel',
config:{
treePanelId: '',
treePanelTitle: '',
treeStore: ''
},


constructor: function(config){
this.initConfig(config);
// return this;
this.title = this.getTreePanelTitle(); //this.treePanelTitle;
this.itemId = this.getTreePanelId(); //this.treePanelId;
this.store = this.getTreeStore(); //this.treeStore;
this.callParent();
},


// title: this.treePanelTitle, <- 'this' will mostly be window object at define time and therefore this.treePanelTitle will mostly be null unless window has property named 'treePanelTitle'
// itemId: this.treePanelId, // mostly will be null
iconCls: 'nav',
// store: this.treeStore // mostly will be null
});

schuc
9 Oct 2012, 11:50 PM
Thanks for the quick reply, vietits!

It seemed to be ok.
Now i have an other strange problem.
When i give my created TreeStore object to the constructor,
i get an
Uncaught TypeError: Cannot call method 'getRootNode' of undefined
when i try to crete the class.

When i create the TreePanel like in the disabled code below, it works fine.
I tried to give the store an unique storeId, but it didn't solve the problem.
As i said: when i create the treePanel like in the disabled code all works fine.

Thanks a lot for any suggestion.
Thomas


Ext.define('ServiceRequest.view.FacilityControl',{
config:{
treeStores : [],
treeStoreUrl : '',
treeModel: '',
treePanelId: '',
treePanelTitle: ''
},

constructor: function(config){
this.initConfig(config);
return this;
},

_createTreeStore: function(treeStoreId){
var store = Ext.create('ServiceRequest.store.TreeStore', treeStoreId);
this.treeStores.push(store);

return store;
},

_createTreePanel: function(panelTitle, panelId){

var treePanel = Ext.create('ServiceRequest.view.FacilityTreePanel', panelId, panelTitle, this._createTreeStore(panelId));


/*
var treePanel = Ext.create('Ext.tree.TreePanel',{

title: panelTitle,
itemId: panelId,
iconCls: 'nav',
store: this._createTreeStore(),
rootVisible : false,
columns:[{
xtype: 'treecolumn',
header: 'Schema',
dataIndex: 'name',
flex: 1
}],
dockedItems: [{
xtype: 'toolbar',
items:[{
text: 'Load Data'
}]
}]
}
})
*/


return treePanel;
}
})

vietits
10 Oct 2012, 12:04 AM
Try to fix your code as below:


_createTreePanel: function(panelTitle, panelId){
// var treePanel = Ext.create('ServiceRequest.view.FacilityTreePanel', panelId, panelTitle, this._createTreeStore(panelId));
var treePanel = Ext.create('ServiceRequest.view.FacilityTreePanel', {
treePanelId: panelId,
treePanelTitle: panelTitle,
treeStore: this._createTreeStore(panelId)
});

schuc
10 Oct 2012, 12:37 AM
unbelivable quick reply!

You're my hero and made my day!
Thanks a lot! All works perfect!