PDA

View Full Version : Probleme with "this" ( Newbie question )



brigot
24 Aug 2012, 1:41 AM
Hello,

While I was trying to use an object, I came across a little problem ....
It is true that it is a shame, but I can not find a way out of the ...

here is my code:



Ext.onReady(function() {

test2();

});

var obj_c, obj_d;
function test2()
{
obj_c = Ext.create('secondTest', { });
obj_d = Ext.create('secondTest', {
test_a : Math.floor(Math.random() *1000 )
,test_b : Math.floor(Math.random() *1000 )
});

obj_c.show({x:100, y:100});
obj_d.show({x:600, y:100});
}

Ext.define('secondTest', {

form : null
,win : null

,config :
{
test_a : 0
,test_b : 0
}

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

// i create a form
this.form = Ext.create('Ext.form.Panel', {
border: false
,fieldDefaults:
{
labelWidth : 100
,labelSeparator : ''
}
,url : 'index.php'
,bodyPadding : 5
,items:
[
{
xtype : 'textfield'
,name : 'the_field'
,fieldLabel : 'field'
,anchor : '100%'
}
]
,bbar :
[
'->'
,{
xtype : 'button'
,text : 'test button'
,tabIndex : 100
,handler : function ()
{

console.info(this.up('form').getForm().findField('the_field').getValue());

// here is my probleme
this.test_a = this.up('form').getForm().findField('the_field').getValue();
this.dump();
}
}
]
});

// and a window
this.win = Ext.create('Ext.window.Window', {
title : 'test box'
,closeAction: 'hide'
,width : 300
,height : 165
,layout : 'fit'
,constrain : true

,items : this.form

});
}


,dump : function()
{
console.info('a => '+this.test_a);
console.info('b => '+this.test_b);
}
,show : function(input)
{
this.win.showAt(input.x, input.y);
}
});


2 windows to display correctly, and if I type via the console:


obj_d.dump ()

the two variables is displayed correctly.
if I type:


obj_d.test_b = 555;
obj_d.dump ()

change is taken into account

but, if I click the button, it does not work ...

test_a is not updated, and he told me


TypeError: this.dump is not a function


Either way, it makes sense, the "this" reference to the button, but then how can I tell him to go back to my main subject? as I have done with this.up ()

evant
24 Aug 2012, 3:35 AM
http://docs.sencha.com/ext-js/4-0/#!/api/Ext.button.Button-cfg-scope

brigot
24 Aug 2012, 3:46 AM
Thank you, but what am I supposed to give parameter?

if I put this, it does not change anything to my problem and the error is the same:



,{
xtype : 'button'
,text : 'test button'
,tabIndex : 100
,scope : this
,handler : function ()
{
// this line run with or without 'scope: this'
console.info(this.up('form').getForm().findField('the_field').getValue());

// here is my probleme
this.test_a = this.up('form').getForm().findField('the_field').getValue();
this.dump();

}
}