PDA

View Full Version : [Fixed] Variable is sometimes undefined



timetrick
14 Aug 2009, 10:54 AM
Hello everybody
and sorry if I did not succeed in finding a thread that may solve my problem,
but I tried, eventough I was not ver certain what search query to look for...
(Also sorry for the title, i could not summarize my problem any better)
I have just started with ExtJS so I might have slightly horrible code

Here is the code:

function openCharacterDetails(charIdent) {
var win;
Ext.Ajax.request({
url: 'character/getCharacterByIdentAjax/' + charIdent,
method: 'POST',
success: function(response) {
character2 = Ext.util.JSON.decode(response.responseText);
console.log(character2);
console.log(character2.name);
win.show();
}
});
var win = new Ext.Window({
items : [{
xtype : 'form',
method : 'post',
id : 'playerdata-form',
labelWidth : 60,
autoHeight : true,
bodyStyle : 'background:#DFE8F6;',
items : [
{
xtype : "textfield",
fieldLabel : "Name",
id : "playerdata-form-name",
value : character2.name, //row 132
editable : false
}]
}]
});
}This function is called when the user (double)clicks on a row in a grid, the parameter ident is barely
the row id.
What it is supposed to do:
Generate a window where a label is shown, filled with the name of a players character, fetched via ajax.
Due to the console.log I know that the ajax request returns successful, but I get the error:
character2 is not defined (this is row 132)
and
win is undefined
in Firebug.
When i click that row again, everything works fine. Why does this happen and how do i solve this?

Another (minor) problem:
When i open the page that sould show me the grid, i sometimes (this is not exactly every second time like above, but seems to be at random) get
ct is null ext-all-debug.js (Zeile 14639)

pdegit
14 Aug 2009, 11:04 AM
Could it be the fact that you have a double declaration of win in your function?
Your declaring it but not setting any value, and then your declaring it again, this time putting a window object in it.

Also, are you sure you're getting a valid JSON response from the server?

And... you're never declaring character2 in your function, but you do reference it immediately when setting the value in the form field (value: character2.name). Although referencing a non existing object should be fine, you can't reference a property of a non-existing object as far as I know.

Animal
14 Aug 2009, 11:09 AM
win gets created before your success callback runs.

READ your code. Read it and follow the sequence of code execution. This is what we do isn't it?

timetrick
14 Aug 2009, 11:25 AM
Ok, thanks alot!
I had a logic error, believing that .show() would not only change the visibility but also
create the object and therefore (re)run its configuration and at this time, the character variable should have been set.
And thanks to you help, I could fix the other problem on my own :D

Animal
14 Aug 2009, 11:40 AM
Good user name BTW.

A lot of being able to work out what your code is doing is being able to read what time frames the code blocks will be executing in.

Asynchronous code execution adds another dimension to dry running your code in your head.