PDA

View Full Version : Setting default scope of keys to 'this' instead of to 'window'



sierk
11 Apr 2008, 11:13 PM
Hi,

In one of my projects I have to catch a lot of different keys and for every key I had to set the Scope to this, to be able to use function from the assosiate panel. With the code below I override the default panel function, which is responsible for creating the keymap in such away that when scope is not set for a key is set to the panel.

Sierk


/* Override Ext.Panel so that scope of keymap is always set to object when not set, instead of window */
Ext.override(Ext.Panel,{
// private
getKeyMap : function(){
if(!this.keyMap){
if(Ext.isArray(this.keys)){
for(var i = 0, len = this.keys.length; i < len; i++){
this.keys[i].scope = this.keys[i].scope || this;
}
} else if (this.keys && !this.keys.scope) this.keys.scope = this;
this.keyMap = new Ext.KeyMap(this.el, this.keys);
}
return this.keyMap;
}
});


Example
When in code below you don't use scope the copy of function is window instead of panel. When using override it will be the new created panel. Setting the scope to something else means that scope is used.


.. = new Ext.Panel({
keys : [{ // F2 = edit
key:113, fn:function(key, e) {
alert('F2' + this.id);
}
}]
});