General Tips and HintsCopying Data Structures from Server to Client DomainIf you want to generate a client-side JavaScript data structure which is an
exact copy of a server-side data structure use the toSource() method.
This method works on any server-side JavaScript object and turns the object
into its object literal syntax equivalent. For example, here a piece of Whitebeam
script generates a piece of client JavaScript that produces a copy of the server
JavaScript object and its values: rb.page.write("<script language='JavaScript'>\n<!--\n");
rb.page.write ("var clientStruct = "+serverStruct.toSource()+";\n");
rb.page.write("//--></script>\n");
There is no real equivalent if you wish to copy a client data structure back
to the server after some user modifications (most client browser JavaScript
implementations do not support the toSource() method). Therefore
you have to rely on the form interface and send back individual data items (loosing
any object structure that may have existed). The best way round this is to store
the original data structure in session data when the form is initially generated
and then modify it based on the changed variables posted back. VariablesDon't forget that JavaScript Date values are in milliseconds. Keep variable names consistent (i.e. client-side, server-side and ROM). Generating Values to use in HTMLThe cleanest way to generate values in HTML from Whitebeam script is to use
the rb:eval="attribute#expression" attribute and the
<rb:eval expr="expression"/> tag within the HTML
on the server. Expression can be anything from the ROM, functions or variables.
For example, the following code inserts the name of the person that has logged
onto the page as a cell in a table: The following is a sample function to be called from within a tag: <rb:script>
// Simple function to get the name of the person who successfully
// accessed this page
function activeUser() {
var me = rb.contact.individualInfo(-1, rb.security.auth.username());
if (me == null) return "";
else return me.name;
}
</rb:script>
This is the HTML that uses the function above: <td> Hello <rb:eval expr="activeUser()"/>, welcome
to site administration </td>
Undefined and Null, Testing for FalseTesting for undefined can cause some pitfalls for the uninitiated and initiated
alike! The undefined value does test equal to null but this is not the same
as saying that it is "false". One problem is that !undefined is still
undefined so you cannot do the following reliably (a test based on formdata
parameters being supplied) if one of the conditions you are testing for is undefined:
if (parameters.extra&&!parameters.add)
testing for not equal to null does work: if (parameters.extra&¶meters.add!=null)
Testing for the presence of parameters in formdata can be done as in if (parameters.add)
if the form data includes the parameter "add" then the test will
be true. |