0

Access Java code and component attributes/properties from Javascript code

asked 2011-08-30 04:49:40 +0800

Macadoshis gravatar image Macadoshis flag of France
39
http://about.me/saad.benb...

Hi,

I looked through the forum to find answers, but it seems that there ain't a load of threads regarding cross-development between components and javascript code.

Well, I have a window declared in my zul as follows :

<window id="myComponent" use="myPackage.myClass" myAttribute="hello">
</window>

In my javascript code, I can successfully access my component object as follows :

zk.Widget.$(jq('$myComponent')[0]);

and also "official" properties, such as :
zk.Widget.$(jq('$myComponent')[0]).id; // --> equals "myComponent"
zk.Widget.$(jq('$myComponent')[0]).getId(); // --> equals "myComponent"
zk.Widget.$(jq('$myComponent')[0]).uuid; // --> equals the uuid value
...etc

But "customized" properties seem unreachable :
zk.Widget.$(jq('$myComponent')[0]).myAttribute; // --> equals "undefined"
zk.Widget.$(jq('$myComponent')[0]).getMyAttribute(); // --> equals "undefined"

How can I access "myAttribute" ?
I already tried adding an attribute markup <attribute name:getMyAttribute>...</attribute> but with no success...

Note that in myClass.java, I implemented the two methods "getMyAttribute()" and "setMyAttribute(...)", as well as I declared the private property "String myAttribute".

Uprising from this question, would it also be possible to access public static constants within javascript code, such as "myClass.MY_WIDTH", "myClass.MY_HEIGHT" ?

Thanks for your replies, I've been struggling for hours round this problem :/

delete flag offensive retag edit

7 Replies

Sort by ยป oldest newest

answered 2011-08-30 06:22:47 +0800

jumperchen gravatar image jumperchen
3909 2 8
http://jumperchen.blogspo... ZK Team

ZK original widget doesn't know the attribute of "myAttribute", you cannot use that way to get the customized attributes.
If you would like to put your own attribute, then receive them from Javascript, you can use this Client_Attribute

link publish delete flag offensive edit

answered 2011-08-30 06:26:35 +0800

mhj gravatar image mhj flag of Brazil
806 1 7

updated 2011-08-30 06:27:22 +0800

hi,
after n tests i only did this way:
.zul

<?xml version="1.0" encoding="UTF-8"?>

<zk xmlns="http://www.zkoss.org/2005/zul">
<window id="wnd" border="normal" width="400px" closable="true" apply="${wndController}" forward="onCreate=onCreateWnd" draggable="false">
        <script type="text/javascript">
	    	function notifyServer(arguments) {
                zAu.send(new zk.Event(zk.Widget.$('$wnd'), 'onNotifyServer',arguments));
	    	}
	    </script>  
</window>
</zk>

.java

public class WndController extends GenericForwardComposer {

  public void onCreateWnd(Event evt) {
    
        Window window = (Window) evt.getTarget();

        if (window.getId().equals("wnd")) {
            window.addEventListener("onNotifyServer", this);
        } else {
            //for others
        }
    }

 @Override
    public void onEvent(Event evt) throws Exception {
        super.onEvent(evt);

        if (evt.getName().equals("onNotifyServer")) {
            serverNotified(evt);
        }
    }
    private void serverNotified(Event evt) {
        Object obj = evt.getData();
        //obj has parameters of javascript
    }

}//class

i hope to have helped
regards

link publish delete flag offensive edit

answered 2011-08-30 08:12:33 +0800

Macadoshis gravatar image Macadoshis flag of France
39
http://about.me/saad.benb...

Hi,

@jumperchen: Sorry but the example is not well-developped enough for me to see how I may insert it in my code. I can't see the javascript part, and also distinguish the javascript part from the java/zul part. Have you already implemented such thing as the example of my first post as applying the principle of the link you provided ?

@mhj: I may be wrong, but I think this trick is to fetch javascript properties from java code, and not the opposite way as I explained it in my example ?

Thanks to both of us.

link publish delete flag offensive edit

answered 2011-08-30 10:56:00 +0800

mhj gravatar image mhj flag of Brazil
806 1 7

updated 2011-08-30 10:57:01 +0800

the code I use to send an attribute that is an applet on client side, as follows:
- When the User clicks a button of the applet (client side) is called the method 'notifyServer (arguments)', this method is in javascript zkoss.
- This method invokes java code (server side) through the component wnd.
if you want to access attributes on server side, see the possibility of keeping them in the client side or pass the logic for the server side, this will depend on your needs.

link publish delete flag offensive edit

answered 2011-08-31 20:59:25 +0800

jumperchen gravatar image jumperchen
3909 2 8
http://jumperchen.blogspo... ZK Team

updated 2011-08-31 21:00:20 +0800

Hi Macadoshis,

Please refer to this example,


index.zul
<zk>
<window id="win" xmlns:a="client/attribute" xmlns:w="client" border="normal" title="hello" a:myAttribute="hello">

<div>Welcome to ZK Fiddle , run it right now!</div>

<button id="btn" label="Test client widget's attribute" w:onClick='zk.log(this.$f("win").$n().getAttribute("myAttribute"))'/>
</window>
</zk>

link publish delete flag offensive edit

answered 2011-09-01 03:14:22 +0800

Macadoshis gravatar image Macadoshis flag of France
39
http://about.me/saad.benb...

updated 2011-09-01 03:15:27 +0800

Hi,

Thank you for your reply. I was hoping it worked while trying it few minutes ago, but it did not :(

In fact, I have to fetch the attribute value from a javascript file, and not from the zul file.

My code is :

// In <myfile.js>
zk.Widget.$(jq('$myComponent')[0]).$n().getAttribute("myAttribute"); // returns null

Moreover, the only attributes available are :

// In <myfile.js>
zk.Widget.$(jq('$myComponent')[0]).$n().attributes;
// returns an array of 3 elements with only attributes : "id", "style" and "class"
// I doesn't expose my own attributes, such as "myAttribute".

Any clue on how to perform it ?

Thanks again jumperchen.

link publish delete flag offensive edit

answered 2011-09-01 03:20:20 +0800

Macadoshis gravatar image Macadoshis flag of France
39
http://about.me/saad.benb...

updated 2011-09-01 04:13:31 +0800

I'm sorry, your code worked, even from a javascript file.

I didn't pay enough attention on the xmlns:a="client/attribute" and a:myAttribute="hello" details.

Thanks again for your support jumperchen, you the man ! :D

PS : By the way, is there a way to make an attribute accessible both client and server side, sharing both namespaces ?
Because as I can now access my attribute client side, myAttribute is no more assigned server side. The following code which used to get mapped with zul automatically is no more :

private String myAttribute;

public String getMyAttribute() {
  return this.myAttribute;
}

public void setMyAttribute(String att) {
  this.myAttribute = att;
}

And I think editing the code into this may become a bit dirty and redundant :

//private String myAttribute;

public String getMyAttribute() {
  final Window me = (Window) this;
  return me.getWidgetAttribute("myAttribute");
}

public void setMyAttribute(String att) {
  final Window me = (Window) this;
  me.setWidgetAttribute("myAttribute", att);
}

Thanks again.

link publish delete flag offensive edit
Your reply
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow

RSS

Stats

Asked: 2011-08-30 04:49:40 +0800

Seen: 870 times

Last updated: Sep 01 '11

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More