Thursday, December 27, 2012

Playing with Form Arguments


Form arguments can be useful.  Of course figuring out when to use form arguments over navigator arguments or the other way around can be a trick.  It really depends on your focus and where that information needs to go within your application.  Still, for now, let's talk about form arguments.

The definition in the documentation that came with starlims says, "  The form parameters are received in the formArguments property of the form object".  Okay, that's useful.  Now we know we need to reference it via the form.formArguments notation.  Okay, what's next.

It further goes on to say: "On loading the form, you must be sure that a value has been passed inside the
formArguments, before trying to retrieve its value, otherwise you will generate an error."

Okay.  So, I need to know ahead of time that the argument exists before I can call it.  Makes sense so far.  Be nice if I could catch what arguments are out there in a general way.  Hmmm.  form.formArguments != null? 

Nope!  That fails when used.  Interesting.  What about assigning it in some fashion?  form.formArguments == null works but I'm looking for something a bit more robust.  How about:

var openArgs = form.formArguments;

Success!  If we do this, it will return a value we can check for and use.  Its null if no formArguments were passed and returns a zero (0) value if we measure its length with lims.len().

Let's see what else the documentation says.

"A programmer must be aware of the way the arguments are sent between forms, as the formArguments property type is Object, and it can hold different types of values inside."

Okay.  Good info.

"So if you send an array as the form argument, you will expect an array in the formArguments property. If you send a string, you will receive a string and so on."

Better info.  So, my resultant form arguments can have different variable types.  Good.  The text gives a warning, though.

"But, for example, if you send a string and treat the formArguments as an array, indexing it (formArguments[2]), if the string has at least two character, you will obtain the second character, otherwise you will generate an error."

Hmmm, so treating the form arguments as a default array might not be the best approach.  Check.

"So before starting to get the form arguments, first you have to assure that arguments have been sent, and if you are expecting an array, the length of that array must be validated, before indexing the array. These are just a few rules a programmer must follow to reduce the possibility of creating errors."

Good points. So, if I use the openArgs approach above, let's try a simple test of passing an array and text.


    var openArgs = frmTest.formArguments;
    if (openArgs != null)
    {
        //code goes here
    }

This gives me a value of 2, letting me know I have two variables.  I can then iterate through those variables and figure out what they are.



No comments:

Post a Comment