Question
· May 26, 2020

Updating a Property from Javascript

I am trying to set a varaible/property within JavaScript and use the value in my method.  The alert method below returns "0".  Do I have the syntax wrong?

/// Storage for current table row

Property currentTableRow As %ZEN.Datatype.string [ InitialExpression = 0 ];

set ..currentTableRow = "1"

&js<

    alert(zenPage.currentTableRow);
>

Discussion (8)1
Log in or sign up to continue

Arnold, it could be a scope problem / zen expression problem.

Try something like this:

Property currentTableRow As %ZEN.Datatype.string(MAXLEN = 5000);
 

Method MyMethodOnServer() [ZenMethod] {

set %page.currentTableRow="1"
&js<alert("zenPage.ExportQuery="+#(%page.currentTableRow)#);>

}

I had trouble with a longer string. For a string that's not a number, try this:

set %page.currentTableRow="line 1"

set str=..QuoteJS(%page.currentTableRow)
&js<alert("zenPage.ExportQuery="+#(str)#);>

 

Make sure that your Cache Method or ClassMethod has the [ ZenMethod ] keyword.

If you're within a %ZEN.Component.page this should work:
Method MyMethod() [ ZenMethod ]
{
    // Within ZEN Pages
    // Set with client code

   &js<
      zenPage.currentTableRow "test"
   >
  // Set with server code
  %page.currentTableRow = "test"
}

If you're within a %ZEN.Component.composite and the variable is also within the composite, this should work:
Method MyMethod() [ ZenMethod ]
{
  // Within Composite Components, here's one way that works as long as your <composite> has an id
  // Set with client code

  S ID = ..id
   &js<
      zen(
"#(ID)#").currentTableRow "test"
   >
  
// Set with server code
  
$this.currentTableRow = "test"
}

I left out the ClientMethod versions for brevity, but basically just use 
zenPage.currentTableRow within ZenPages, and this.currentTableRow within Composite Components.

Hope this helps!