PDA

View Full Version : Scoping issue: update class field



mike1993
4 Feb 2008, 9:23 AM
I have a "base" class (a window with a form) and "extended" class. One of the function calls in extended class is an overloaded renderer for the form object.
Upon form load request completion renderer supposed to update a class field - 'total'. (this var will be submitted w/ the form). And I just cannot make the changed var be visible within extended class.



AllocationWindow = function( config ) {
if( config ) Ext.apply( this, config );
this.form = new Ext.FormPanel({....};
this.cli_id = -1;
this.total = -1;
};

Ext.extend( AllocationWindow, Ext.Window, {
getSomeData: function() {
form.getEl().getUpdater().setRenderer( {
render: function(el, response, updater, callback) {
var resp = Ext.decode(response.responseText); ...

// update property 'total'
this.total = resp.data.length; <---------- !!!!
}
});

},

onUpdate: function() {
Ext.Ajax.request({
url: 'client.cgi',
params: {'cli_id' : this.cli_id, 'total' : this.total}, <--- use new value here
success: successHandler,
failure: failHandler,
scope: this
});
},



How do I set/apply new value to 'total' from within render object?

Thanks!

4 Feb 2008, 10:26 AM
A cheater method could be:



AllocationWindow = function( config ) {
if( config ) Ext.apply( this, config );
this.form = new Ext.FormPanel({....};
this.cli_id = -1;
this.total = -1;
};

Ext.extend( AllocationWindow, Ext.Window, {
getSomeData: function() {
function setTotal(data) {
if (data) {
this.total = data;
}
}
form.getEl().getUpdater().setRenderer( {
render: function(el, response, updater, callback) {
var resp = Ext.decode(response.responseText); ...

// update property 'total'
setTotal(resp.data.length); <---------- !!!!
}
});

},

onUpdate: function() {
Ext.Ajax.request({
url: 'client.cgi',
params: {'cli_id' : this.cli_id, 'total' : this.total}, <--- use new value here
success: successHandler,
failure: failHandler,
scope: this
});
},

mike1993
4 Feb 2008, 11:41 AM
No go. This way 'total' is set for the current browser window, not the AllocationWindow object.

mike1993
4 Feb 2008, 11:46 AM
I am not sure how this comes into a play, since I create window object in my base class



AllocationWindow = function( config ) {
this.form = new Ext.FormPanel({....};
this.cli_id = -1;
this.total = -1;

AllocationWindow.superclass.constructor.call(this, {
title: '....',
....
buttons: [{.. Update button}, {.. Cancel button}],
items: this.form
});
};

mike1993
5 Feb 2008, 7:07 AM
Gto it to set class instance to a new value, finally.
But is this the only way to go? really curious....


Ext.extend( AllocationWindow, Ext.Window, {
getSomeData: function() {

var myObj = this;

form.getEl().getUpdater().setRenderer( {
render: function(el, response, updater, callback) {
.....
/***********************************/
Ext.apply(myObj, {'total': resp.data.length } );
/***********************************/
}
}
});