PDA

View Full Version : Passing variables to javarpc methods


rob
10-27-2004, 12:46 PM
I'm tring to pass a variable to a javarpc. I've read the doc on javarpc, but what they pass are strings, 'some text', or ints, 23, etc...

This is some of what I've tried:

First I set up the rpc

<javarpc name="ce" scope="webapp" autoload="true" classname="TestJava">

<method event="onerror" args="err">
Debug.write("----DOH------");
Debug.write("onerror:", err)
</method>

<method event="onload">
Debug.write("----------");
Debug.write("Loaded TestJava...");
Debug.write(this.proxy);
Debug.write("----------");
</method>

</javarpc>

...

<button width="80" x="200" y="10">Update
<method event="onclick">
Debug.write("----------");
Debug.write("----Hit Update------");
parent.datapath.updateData();
var a = parent.datapath.serialize();
Debug.write(a);
this.getInfo.invoke();
</method>
<remotecall funcname="getInfo" remotecontext="$once{ ce }" >
<param value="escape(a)"></param>
</remotecall> </button>


...


Any ideas on what I might be doing wrong?

pablo
10-28-2004, 08:41 AM
Hi Rob,

What kind of error are you seeing? Can you post what the java argument type for getInfo() is?

Thanks,
pablo

rob
10-28-2004, 09:43 AM
The error that the above code gives is:

copy-of-hello.lzx:61: reference to undefined variable 'a'

getInfo on the Java side is expecting a String. I've tested the Java, and it works as it should - if I call getInfo() via someother route it works fine.

If I change getInfo() so that it doesn't take any args, then my call to getInfo() works. I just can't figure out how to pass the var a.

Thanks

pablo
10-28-2004, 10:00 AM
The variable a is local to the onclick method. I think what you want to do is this:


<button width="80" x="200" y="10">Update
<attribute name="a" type="string" />
<method event="onclick">
parent.datapath.updateData();
this.setAttribute('a', escape(parent.datapath.serialize());
Debug.write(a);
this.getInfo.invoke();
</method>
<remotecall funcname="getInfo" remotecontext="$once{ ce }" >
<param value="${parent.parent.a}"></param>
</remotecall>
<!-- ... -->
</button>


Let me know if this works.

Cheers,
pablo

rob
10-28-2004, 10:15 AM
Thanks for the quick reply.

I replaced your code with mine, and here's the error I get:


copy-of-hello.lzx:66: reference to undefined variable 'a'
----------
Loaded TestJava...
«object#0| {}»
----------
copy-of-hello.lzx:61: reference to undefined property 'a'
%3Cprofessor%3E%3Cfirst%5Fname%3EJohnasdf%3C%2Ffir st%5Fname%3E%3Clast%5Fname%3EDoe%3C%2Flast%5Fname% 3E%3Caddress%3Ejohn%5Fdoe%40someEmail%2Ecom%3C%2Fa ddress%3E%3Cphone%3E555%2D555%2D1234%3C%2Fphone%3E %3Coffice%3EMEB%20100%3C%2Foffice%3E%3C%2Fprofesso r%3E
rpc/rpc.lzx:220: reference to undefined property 'value'


(... the junk characters are the string I'm trying to pass, it used to be XML, but somehow lost it's markup when I tried it this way...)

What I'm trying to do is to keep a simple XML database. Since Laszlo can't write to the file system, I just want to have laszlo read the XML, then update it, then serialize the new XML and pass it to a java program that will overwrite the previous version of the XML data. Is there a better way to go about this?

Thanks

pablo
10-28-2004, 11:11 AM
Can you post your LZX code? I'm curious to see why you're getting the undefined variable warnings and think that has something to do with your problem.

pablo

rob
10-28-2004, 11:29 AM
Sure, thanks for the help, I'm just learning this, so I appreciate your patience.


<canvas height="500" width="600" debug="true">

<security>
<allow>
<pattern>TestJava</pattern>
</allow>
</security>

<dataset name="myData" autorequest="true" type="http" src="prof.xml"/>



<!-- //////////////////////////// RPC SETUP ////////////////////////////////////// -->
<javarpc name="ce" scope="webapp" autoload="true" classname="TestJava">

<method event="onerror" args="err">
Debug.write("----DOH------");
Debug.write("onerror:", err)
</method>

<method event="onload">
Debug.write("----------");
Debug.write("Loaded TestJava...");
Debug.write(this.proxy);
Debug.write("----------");
</method>


</javarpc>




<!-- //////////////////////////////////// ADMINISTRATION WINDOW /////////////////////////////////////////-->
<dataset name="dset" src="prof.xml"/>
<class name="adminWindow" width="400" height="300" resizable="true" closeable="true" bgcolor="white" title="Site Administration" extends="window">
<simplelayout axis="y"/>

<!-- EDIT PERSONAL INFO -->
<text onclick="parent.updateContact.setVisible(!parent.updateCont act.visible);" fgcolor="blue" x="15"><u>Edit Personal Info</u></text>
<view name="updateContact" visible="false" x="20" height="120" datapath="dset:/professor">
<text y="10">First Name:</text>
<edittext name="firstName" datapath="first_name/text()" x="80" y="10"/>
<text y="35">Last Name:</text>
<edittext name="lastname" datapath="last_name/text()" x="80" y="35"/>
<text y="60">Phone:</text>
<edittext name="phone/text()" datapath="phone/text()" x="80" y="60"/>
<text y="85">Email:</text>
<edittext name="email/text()" datapath="address/text()" x="80" y="85"/>


<button width="80" x="200" y="10">Update
<attribute name="a" type="string" />
<method event="onclick">
parent.datapath.updateData();
this.setAttribute('a', escape(parent.datapath.serialize()));
Debug.write(a);
this.getInfo.invoke();
</method>
<remotecall funcname="getInfo" remotecontext="$once{ ce }" >
<param value="${parent.parent.a}"></param>
</remotecall>


<remotecall funcname="getInfo" remotecontext="$once{ ce }" >
<param value="escape(a)"></param>
</remotecall>
</button>
<button width="80" x="200" y="40">Delete

<method event="onclick">
parent.datapath.deleteNode();
</method>
</button>
</view>
<!-- END EDIT PERSONAL INFO -->
</class>





<font name="helmet" src="helmetr.ttf"/>
<!-- MAIN VIEW -->
<view height="600" width="600" bgcolor="white">

<simplelayout axis="y" spacing="10"/>
<view height="50" width="600">
<!-- TITLE -->
<text font="helmet" fontsize="25" x="10">Dr. John Doe</text>
<!-- ADMIN BUTTON -->
<button text="Admin" x="500" y="5">
<method event="onclick">
var a = new adminWindow();
</method>
</button>
</view>
<!-- INFO VIEW -->
<text fgcolor="red" x="10">Personal Info:</text>
<view height="125" width="230" bgcolor="0xe5e5e5" x="10">
<simplelayout axis="x" spacing="5"/>
<view height="125" width="50" bgcolor="0xe5e5e5">
<simplelayout axis="y" spacing="10"/>
<text>Name:</text>
<text>Phone:</text>
<text>Email:</text>
<text>Office:</text>
</view>
<view height="125" width="200" bgcolor="0xe5e5e5" x="10">
<simplelayout axis="y" spacing="10"/>
<view height="15" width="200" bgcolor="0xe5e5e5">
<simplelayout axis="x" spacing="0"/>
<text datapath="myData:/professor/first_name/text()" width="${this.width-20}"/>
<text datapath="myData:/professor/last_name/text()"/>
</view>
<view bgcolor="0xe5e5e5" height="50" width="200">
<simplelayout axis="y" spacing="5"/>
<text datapath="myData:/professor/phone/text()"/>
<text datapath="myData:/professor/address/text()" width="200"/>
<text datapath="myData:/professor/office/text()" width="200"/>
</view>
</view>
</view>
<!-- END INFO VIEW -->

</view>
</canvas>


So I should be able to use variables in this way? I understand that in standard JavaScript, global variables can be seen from anywhere in the document, so how are globals different with Laszlo.

Thanks

pablo
10-28-2004, 01:04 PM
Try removing the second getInfo method declaration and let me know how it goes!


So I should be able to use variables in this way? I understand that in standard JavaScript, global variables can be seen from anywhere in the document, so how are globals different with Laszlo.


Variables declared in script using var are local. Here's code to explain how variables are scoped:


<canvas debug="true">

<debug height="300" />

<method event="oninit">
Debug.write('localVar:', localVar);
Debug.write('fooVar:', fooVar);
Debug.write('foo.fooVar:', foo.fooVar);
Debug.write('globalVar:', globalVar);
Debug.write('globalVar2:', globalVar2);
</method>

<view name="foo">

<!-- fooVar is local to the view named foo -->
<attribute name="fooVar" value="foo variable" type="string" />

<method event="oninit">
// variable declared with var are local
var localVar = "a local variable";

// the following two are global
globalVar = "global variable";
global.globalVar2 = "explicit global variable";
</method>
</view>


</canvas>


pablo

rob
11-02-2004, 12:36 AM
Thanks for the help Pablo, and sorry for the sloppy code.

I believe I've got my scope problems sorted - I made the variable I wish to use as an argument to my RPC, and I can see it everywhere.

However, the problem comes when I try to use it in the <param> tag of the remote call:


<remotecall funcname="getInfo" remotecontext="$once{ ce }" >
<param value="${escape(a)}"></param>
</remotecall>


This will give me the error "rpc/rpc.lzx:220: reference to undefined property 'value'"

If instead of trying to pass the variable, I pass a string, it will work fine (ie <param value="'Testing'"></param>

Any ideas where I missing it here? Is my variable still out of scope? I can see it elsewhere in the code.

Thanks again.

rajs1
11-02-2004, 03:10 AM
Firstly, I have not read this entire thread in great detail so forgive me if my answer is not totally suitable.

But this is what I do to process a "comments" form from my web page.

<javarpc name="commentsproc" scope="session" autoload="true" classname="ContactProcessor">
<method event="onerror" args="err">
Debug.write("err:"+err);
a7.open();
</method>
<method event="onload">
Debug.write("commentsproc:"+this.proxy);
</method>
<method event="ondata" args="res">
a6.open();
</method>
<remotecall name="cmnts" funcname="sendComments"/>
</javarpc>


As you might have guessed, a6 and a7 are alerts.

Then in order to invoke the rpc call:


<method name="checkandsubmitcontact">
var title = canvas.trim(titleInput.getAttribute('text'));
var lastname = canvas.trim(lastnameInput.getAttribute('text'));
var companyname = canvas.trim(companyInput.getAttribute('text'));
var email = canvas.trim(emailAddressInput.getAttribute('text') );
<![CDATA[
...
do various things if mandatory fields are not filled
...
]]>
var array = [
titleInput.getAttribute('text'),
firstnameInput.getAttribute('text'),
middlenameInput.getAttribute('text'),
lastnameInput.getAttribute('text'),
companyInput.getAttribute('text'),
addressInput.getAttribute('text'),
phoneInput.getAttribute('text'),
faxInput.getAttribute('text'),
emailAddressInput.getAttribute('text'),
webInput.getAttribute('text'),
commentsInput.getAttribute('text')
]
commentsproc.cmnts.invoke(array)
</method>



Hope this helps.

Cheers
-raj

pablo
11-02-2004, 02:11 PM
Hi Rob,

You need to use a getter for the variable 'a' instead of a constraint. Try this:


<remotecall funcname="getInfo" remotecontext="$once{ ce }" >
<param>
<method name="getValue">
return escape(a);
</method>
</param>
</remotecall>


getValue() will be called whenever getInfo() is invoked to get at the param.

Read my reply on this post for an explanation on how constraints behave:

http://www.laszlosystems.com/developers/community/forums/showthread.php?s=&postid=4885

Let me know how it goes!

pablo

rob
11-03-2004, 01:51 PM
Thanks Raj, Pablo.

I used the getter as you suggested Pablo and it's straight now. My first hurdle was my variable being out of scope, and then it was not understanding what the param tag can handle.

Enjoying Laszlo thanks to this board. Lots of answers here.