JavaScript string type error

This works:

elements['name_last'].requestFocus();

This also works:

var field_var = 'name_last';
elements[field_var].requestFocus();

This doesn’t work:

var field_var = '_1_name_last';
field_var = field_var.slice(3);
elements[field_var].requestFocus();

The string in field_var in the last example is identical to the string returned in the first two examples. The request focus function won’t work with it though.

It is also a problem with:

forms.controller_main.elements[myVar].enabled = false;

If I do any string manipulation to myVar at all before hand it will not work. I have to make it work like this instead:

if (tableName == 'client')
{
	forms.controller_main.elements['client'].enabled = false;
}

This bug is a pain because if it worked I could send all kinds of parameters through to the method via the application.getMethodTriggerElementName() and split into different variables in the method. Saving me a lot of lines of code and cutting down on multiple scripts for the same function throughout my solution.

  • david

Another example:

//Generic GTRR method (almost there)
//var input =  main_appraisal_form__client_to_appraisal
//which is the name of the object that triggers this method

var input = application.getMethodTriggerElementName();
var currentForm = currentcontroller.getName();

var formPosition = utils.stringPosition(input, '__', 1, 1);
var GTRRForm = utils.stringLeft(input, formPosition - 1);
var relationship = utils.stringMiddle(input, formPosition + 2, input.length);
var relationship = 'forms.' + currentForm + '.' + relationship;

//this line works:
	forms[GTRRForm].controller.showRecords(forms.main_client_form.client_to_appraisal);

//this line is what I want but it does NOT work:
	//forms[GTRRForm].controller.showRecords(relationship);
//even though var relationship = 'forms.main_client_form.client_to_appraisal'
  • david

what version of servoy do you use?
because if i do this:

var field_var = ‘name_last’;
var element1 = elements[field_var];
application.output(element1);

field_var = ‘_1_name_last’;
field_var = field_var.slice(3);
var element2 = elements[field_var];
application.output(element2);

it outputs fine twice the same element?
Doesn’t it at youre place?

RC11 on Windows XP.

Yes, the output is the same also in my examples. It’s just that when you start doing things like:

forms.controller_main.elements[myVar].enabled = false;

the value of myVar can look the same in output but one version (the version where you parsed the string in some fashion) will not work in the above example.

  • david

I just tested your code but it works fine. (rc11)
Please re check if your slice output matches your element name.

what is myvar in youre example:

forms.controller_main.elements[myVar].enabled = false;

??

How is it different then from my code:

var element2 = elements[field_var];

?

what happens when you do this:

application.output(forms.controller_main.elements[myVar])

I have the same problem.

I have two variables:
globals.downForm = ‘individual’
globals.downRel = ‘forms.species.species_to_individual’

This line of code works:
forms[globals.downForm].controller.showRecords(forms.species.species_to_individual);

This line does not:
forms[globals.downForm].controller.showRecords(globals.downRel);

It produces the following error message:
“Error loading primary key data, query must start with ‘SELECT’.”

The correct form is displayed (individual), but it has ignored the relationship and displays all records instead of only those which are related.

I am using Servoy 2.0.2 on Windows XP.

Just an inexpierenced thought

Might have somehting to do with java and xp. I have been getting java main class errors when booting my PC. I googled the errors and found out that alot of people are having problems with xp and java.

gevans:
I have the same problem.

I have two variables:
globals.downForm = ‘individual’
globals.downRel = ‘forms.species.species_to_individual’

This line of code works:
forms[globals.downForm].controller.showRecords(forms.species.species_to_individual);

This line does not:
forms[globals.downForm].controller.showRecords(globals.downRel);

use:
globals.downRel = forms.species.species_to_individual (without quotes)
There is a big difference beteen using plaintext (all things beteen qoutes) and referencing objects like related foundsets.

Hi Jan,

Thanks for your advice. I had thought I had needed the quotes to refer to the relationship name, but I can see it is better to refer to the relationship itself.

I have taken the quotes out, so the variable (globals.downRel) now evaluates to 1. Unfortunately the code still doesn’t work, and the same error message appears.

Do you have any other suggestions??

how can globals.downRel ever evaluate to 1? an relationobject is not a value, are you sure species_to_individual is a relation?

I thought evaluating to 1 was rather strange too. I’m quite new to Servoy, and haven’t used objects as variables before, so thought perhaps there was something I was missing. I have now worked out where the 1 is coming from: it is the PK of the record in the found set. Originally there was only one related record, I have now added two more, so the variable now evaluates to “1 6 8”. But I still cannot explain why it is doing this!

I am certain it is a valid relation: I am copying the name directly from the tree in the method editor, so there are no typos, and when I put the relation name directly into the code (as in the first line above) it works perfectly.

what is the type of the global globals.downRel? if this is not of type media the value is converted when assigned!
So when globals.downRel is of type text and assigning species_to_individual it will do a toString()…

Why are you using a global anyway?
you could use:

var downRel = forms.species.species_to_individual
forms[globals.downForm].controller.showRecords(downRel)

globals are only needed when you want to display info on forms or hold information for long period.
Note: if you do not clear globals yourself you could create memory leaks, espesially when assigning foundsets to globals

Oops! Yes, globals.downRel was type text… I changed the type to media, and that seemed to fix things. Interestingly, the first couple of times through the debugger it evaluated to the relationship, then it reverted to doing the string conversion and the script stopped working. So I found a different way!

I was using global variables so I could use them in multiple methods, but have put some more thought into it and have now found a way to use form variables instead. The variable storing the relationship still evaluates it as a string, but the script works so that’s OK!

Thank-you very much for your help.