PDA

View Full Version : Ext.Apply and if-then-else statements in Sencha



JA12
14 Nov 2011, 8:28 AM
I have two problems.
I'm trying to pick up a variable defined in the calling HTML (javascript variable).


Ext.apply(myID, messageId, {myID: '0'});

messageId is definitely visible and contains the value. The problem is, that myID remains undefined after the statement is supposed to execute. Is this not possible to do?

My next question, is the following code possible?


helpPopup = new Ext.Panel({<snip>
style: function() {
var myOrientation = function(value) {
var rec = messages.getById(myID);
return rec ? rec.get('orientation') : '';
}
switch (myOrientation) {
case 0: return 'thought-tl.gif'; break;
case 1: return 'thought-tr.gif'; break;
case 2: return 'thought-bl.gif'; break;
case 3: return 'thought-br.gif'; break;
};
},
<snip>

Execution skips over this, and style remains undefined. If not, how does one perform the same functionality in sencha script?

arthurakay
14 Nov 2011, 1:19 PM
Your first question: if myID is undefined before that line, it won't work as expected. You should test to see if it has a value first.


if (!myID) { myID = {} };
Ext.apply(myID, messageId, { ... });


Your second question: I think you need to call the function (note the parentheses!):

switch ( myOrientation() ) { ... }

arthurakay
14 Nov 2011, 1:20 PM
Although, myOrientation() might return an empty string... so your switch statement might not work correctly.

JA12
15 Nov 2011, 7:00 AM
I've taken the assign operation out of the Panel declaration


var rec = messagesList.getById(myID);
myOrientation = rec ? rec.get('orientation') : 0;
if (!myOrientation) { myOrientation = 0; }
switch (myOrientation ) {
case 0: myImage = 'thought-tl.gif'; break;
case 1: myImage = 'thought-tr.gif'; break;
case 2: myImage = 'thought-bl.gif'; break;
case 3: myImage = 'thought-br.gif'; break;
};

The code now executes, but the getById doesn't return anything (myID=4). So what on earth is going on?

arthurakay
15 Nov 2011, 7:11 AM
I modified your original code as follows. See my comments



//YOU PASS A VALUE TO THIS METHOD, BUT NEVER USE IT...
var myOrientation = function(value) {

var rec = messages.getById(myID); //I HOPE messages IS ACCESSIBLE FROM HERE...

return rec ? rec.get('orientation') : ''; //NOTICE THIS MAY RETURN AN EMPTY STRING
}

//CALLING myOrientation() WILL EVALUATE THE RETURN VALUE
//BUT YOU DON'T PASS THE value PARAMETER... SEE MY FIRST COMMENT ABOVE
switch (myOrientation() ) {
case 0:
return 'thought-tl.gif';
break;
case 1:
return 'thought-tr.gif';
break;
case 2:
return 'thought-bl.gif';
break;
case 3:
return 'thought-br.gif';
break;

default: //HAVE A HANDLER FOR THE EMPTY STRING CASE
return 'Something...';
break;
};


Your debugger probably didn't go into your function definition because you reassigned the reference value in that last example you posted. That last example is actually a hot mess from the looks of it... hopefully this helps you.

JA12
15 Nov 2011, 8:02 AM
Your example still doesn't work for me. I know it's not your code, something else is going on...

The messagesList.load doesn't appear to load any records, the "data" in the watch list doesn't have any items.

I've simplified it. Stopped using objects and just use variables:

rec = messagesList.getById(myID);myOrientation = rec ? rec.get('orientation') : 0; // <--- getById doesn' t get anything
if (!myOrientation) { myOrientation = 0; }
switch (myOrientation ) {
case 0: myImage = 'thought-tl.gif'; break;
case 1: myImage = 'thought-tr.gif'; break;
case 2: myImage = 'thought-bl.gif'; break;
case 3: myImage = 'thought-br.gif'; break;
default: return 'blank.gif'; break;
};

I suspect that when my question http://www.sencha.com/forum/showthread.php?155144-Problem-loading-data-store is resolved, this one might well get resolved at the same time!