I come from java background,and quite new to javascript while using Extjs4.2 .
What troubled me a lot is ,undefined function or variable error,like the example following.
Say I use listeners in tree store to listen tree node expand event like below :
//define a model
Ext.define('Person', {
extend: 'Ext.data.Model',
fields: [
{ name: 'id', type: 'int' },
{ name: 'name', type: 'string' }
]
});
//create a tree store
var store = Ext.create('Ext.data.TreeStore', {
model: 'Person',
root: {
name: 'People',
id : -1,
expanded: false
},
proxy: {
type: 'ajax',
api: {
create: 'createPersons',
read: 'readPersons',
update: 'updatePersons',
destroy: 'destroyPersons'
},
reader : {
type : "json",
root : "children"
}
},
listeners : {
'beforeexpand' : function(node,eOpts){
console.log("requesting child of node "+node.get("id"));
},
'expand' : function(node, eOpts ) {
console.dir(this);
//why getById undefined
console.info(this.getNodeById("1").get("name"));
}
}
});
My problem is :
Since console.dir(this) print (in expand callback function) show :
$className
"Ext.data.TreeStore"
alias
["store.tree"]
getNodeById
function(a)
$extIsFunction
true
$isFunction
1
$name
"getNodeById"
$owner
A()
__proto__
function()
$extIsFunction
true
$isFunction
1
we know ,here 'this' refer to Ext.data.TreeStore . And I looked up the API doc ,also found the getNodeById method,its declaration is :
getNodeById( id ) : Ext.data.NodeInterface
Returns the record node by id
Parameters
id : Object
Returns
Ext.data.NodeInterface
But why I still get the error ? :
TypeError: this.getNodeById(...) is undefined
-->console.info(this.getNodeById("1").get("name"));
this
is being changed by another function to a different object? Instead of usingconsole.dir(this)
, see whatconsole.log(typeof this.getNodeById)
returns – Davidget("name")
on an undefined result. – Dennis