-
15 Feb 2013 5:10 AM #1
Answered: Problems with scope.
Answered: Problems with scope.
Hello All !
I have following example of code an d I am fail to understand what matter is. As you see if apps worked normally the buttons text will be changed by click on it. But there is problem, framework cannot see my method setButtonsText().
Ext.create('Ext.panel.Panel',{
renderTo:Ext.getBody(),
title:'Example',
width:200,
height:300,
items:[
{
xtype:'textfield',
fieldLabel:'Text'
},
{
xtype:'button',
text:'Change me by click',
handler: setButtonsText()
}
]
,
setButtonsText:function(){
var text=Ext.ComponentQuery.query('panel > textfield')[0].getValue();
Ext.ComponentQuery.query('panel > button')[0].setTitle(text);
}
})Last edited by kramal; 15 Feb 2013 at 5:11 AM. Reason: to make our life easier
-
Best Answer Posted by mixowhere this is pointer to your panel object, so there scope is just property of button but not function context objectCode:
{ xtype:'button', text:'Change me by click', scope: this, handler: function() { this.scope.setButtonsText(); } }
-
15 Feb 2013 6:31 AM #2
Try this:
Code:Ext.create('Ext.panel.Panel', { renderTo: Ext.getBody(), title: 'Example', width: 200, height: 300, items:[{ xtype:'textfield', fieldLabel:'Text' }, { xtype:'button', text:'Change me by click', handler: function() { this.up('panel').setButtonsText(); } }], setButtonsText: function() { var text = this.down('textfield').getValue(); this.down('button').setText(text); } });
-
16 Feb 2013 12:17 AM #3Sencha - Community Support Team
- Join Date
- May 2012
- Location
- Istanbul
- Posts
- 1,331
- Vote Rating
- 77
- Answers
- 124
hi kramal
I have made small changes in your code have a look
Code:Code:Ext.create('Ext.panel.Panel',{ renderTo:Ext.getBody(), title:'Example', width:200, height:300, items:[ { xtype:'textfield', fieldLabel:'Text', id:'text' }, { xtype:'button', text:'Change me by click', scope:this, handler: function(button){ var value=Ext.getCmp('text').getValue(); button.setText(value); } } ] })
sword-it.com, Sencha Developer House in Turkey - Istanbul University Technopark Suite 204.
-
18 Feb 2013 12:29 AM #4
Hello All!
1)Sword_it your advise is helpful, but I read that using of Ext.getCmp() is unrecomended style.In your case I would to use better Ext.ComponentQuery.query().
2)droessner your advise is good , But I want to get something like
...
scope:this,
...
this.setButtonsText()
Thanks !
-
18 Feb 2013 1:14 AM #5
well it'll be funny hack but you can define
in button configCode:scope:this
and in btn handler you can access to scope like this.scope.setButtonsText
-
18 Feb 2013 1:43 AM #6
-
18 Feb 2013 2:32 AM #7
where this is pointer to your panel object, so there scope is just property of button but not function context objectCode:{ xtype:'button', text:'Change me by click', scope: this, handler: function() { this.scope.setButtonsText(); } }
-
18 Feb 2013 2:37 AM #8
thank you very ma ..!
I understand it


Reply With Quote