I've searched a little about the scope problem and found some stuff but I'm not sure they work for my case. I'm really confused atm so I thought I might as well ask directly to the forums.
I have a tree panel. Inside that tree panel I have a dockedItem of xtype: 'textfield'. That textfield takes a value and uses it as a parameter in an ajax request via Ext.Ajax.Request inside a handler for the specialkey event. The server returns a JSON object on its response that gets decoded and contains folder IDs of the nodes of the tree. Inside the SUCCESS config of the ajax request there is a function I want to run that has to refer to the nodes of the tree via getNodeById(IdFromAjaxResponse) and expand them. The problem is that I do not know how to refer to those nodes. I tried this.getStore().getNodeById but it turns out 'this' refers to the window (I guess my container for the tree panel???). How do I refer to the tree panel or the tree store? I do not want to use direct referrals like getCmp etc.
Some code to help:
Ext.define('treePanel', {
extend: 'Ext.tree.Panel',
alias: 'widget.folderTreePanel',
//title: 'Folder Tree',
displayField: 'name',
rootVisible: false,
store: 'treeStore'
dockedItems: {
xtype: 'textfield',
name: 'Search',
allowBlank: true,
enableKeys: true,
listeners: {
specialkey: function (txtField, e) { //This handler will essentially take the search value and return the path from the DB
if (e.getKey() == e.ENTER){
var searchValue = txtField.getValue();
Ext.Ajax.request({
url: 'MyServlet',
params: {
caseType: 'search',
value: searchValue
},
success: function(response) {
response = Ext.decode(response.responseText);
var node, i=0;
expandFn = function () {
This is where I have a problem->node = this.up('treePanel').getStore().getNodeById(response.IDs[i].folderId);
node.expand();
i++;
if (i >= response.IDs.length-1) return;
expandFn();
}
}
});
}
}, scope: this
}
},
columns: [{
xtype: 'treecolumn',
text: 'Folder Name',
flex: 2,
sortable: true,
dataIndex: 'name'
},{
text: 'Folder ID',
dataIndex: 'id',
flex: 2,
sortable: true
}]
});
EDIT: I found the answer. Each event passes the instance that triggered it as an argument into the handler function. In this case txtField
refers to the textfield itself. I can then traverse the inheritance and find the panel.
However there is a new problem now. The expandFn()
gets called ONCE and then stops because 'node' is undefined. It is supposed to recurse until there are no more items in the response array. Again I think this is a scope problem but I'm really confused and I don't seem to see the goes wrong...