Question
· Aug 11, 2016

Calling javascript method from Zen method, with parameters

I'm running into difficulty calling a javascript method from a Zenmethod.  I pass in a parameter, but in the javascript method the parameter is undefined.  Here is the code:

...

<button id="btnSave" caption="Save " onclick="zenPage.doSave();" controlClass="bButton"/>

...

Method doSave() [ ZenMethod ]
{

          If (..ID = "AddNew"){
                    //do save code here                   
                    &js<zenPage.doAddNew();>      //this works just fine, but there's no parameter

          }Else{
                    //do update code here

                    //call javascript method from ZenMethod
----->         &js<zenPage.doReturn('1');>     //this calls doReturn, but the parameter '1' is undefined when it gets there
                    //do %page.doReturn("1")         //NO
}

 

ClientMethod doReturn(silent) [ Language = javascript ]
{
           alert('silent = ' silent)     //shows as undefined

}

 

 

I did not choose to set the methods up like this; they exist already.  I just need to call the doReturn method, which already exists as a javascript method, in order to return to another page. 

 

 

If the ZenMethod doSave were a javascript method, would it work better?

Thanks,

Laura

Discussion (9)0
Log in or sign up to continue

No.  If the Zenmethod is a javascript method, it does not work any better.  The parameter *silent* is showing up in firebug (in FireFox) as "1", and then is immediately shown in the alert box as undefined.

 

I was rifiling through some of the demo code in 2016, but I could not quickly find an example of a Zenmethod calling a javascript method with a parameter.  All the javascript methods with parameters are called from html, not Zenmethods.

Aside from skipping the parameter, any thoughts?

Yes, as written it should be fine, as indicated from tests run on w3schools.com.  This should work too:

&js<var silent='1'; zenPage.doReturn(silent);>

 

but alas, the compiled code (?) is not getting it. I have not yet tried a simple zen page, but I suspect it's not the code... how do I remove the compiled code and recompile? What else is there??

Thanks for your thoughts.

Laura

I thought it was kinda funny that the compiled javascript looked like this:

 

self.CallReasnModify_doReturn = function(silent,silent) {
  confirm('silent = ' + silent)

 

but I thought it was just how InterSystems generated it.  I changed the name of the variable to 'foo' and it then looked like this:

 

self.CallReasnModify_doReturn = function(foo,silent) {
      confirm('silent = ' + foo)
    
    

And it worked. ?  Brings me back around to a compile thing. What's the deal with the extra parameter in there?

If you copy the client method to a client method with a different name, do you still have the duplicate parameter in the new client method code?

What do you get for the output from the following commands? Replace "YourApp.Page" with the name of your page or component class (without any .cls or .zen extension).
Set sig=##class(%Dictionary.CompiledMethod).IDKEYOpen("YourApp.Page","doReturn").FormalSpecParsed
Write $LL(sig),! zzdump sig

Yes, yesterday I had the same method in both the child class and th parent class.  It wasn't working in either class, but I think it was because it existed in both.  I have since removed the meothod from the child class, and this is the sig dump:

 

2
 
0000: 1D 01 06 01 70 55 52 4C 11 01 25 4C 69 62 72 61         ....pURL..%Libra
0010: 72 79 2E 53 74 72 69 6E 67 02 01 02 01 1F 01 08         ry.String.......
0020: 01 73 69 6C 65 6E 74 11 01 25 4C 69 62 72 61 72         .silent..%Librar
0030: 79 2E 53 74 72 69 6E 67 02 01 02 01                     y.String....

 

Interesting.  Looks like just the two params now, and no doubles.

 

So, child classes cannot have the same javascript method name as a parent class? Oh wait, there's no overloading in Cache, so perhaps, even though that's more like overriding, it didn't work?

 

Can we open a discussion on child class properties?  I added some to a mid-level class (it has a parent and a grandparent, and a bunch of children); when I added two properties to it, the grand-parent class was unable to retrieve a property correctly.  When I remove the properties, the grand-parent class is fine. Seems similar to having a method in a parent class with the same name as a method in a child class.

This should all work fine. Can you make a standalone example (as small as possible) that demonstrates the method signature problem and attach it to this thread?

The property question while also related to inheritance, is probably a different discussion, and I'd recommend working up a small example about that and starting a new discussion, but I'll leave it to your judgment if you think it would better fit here.

In general, classic single inheritance in Zen should work exactly as expected -- and has worked in my experience pretty much from day one.