0

How to access client-side component attributes from the composer

asked 2011-05-04 11:51:03 +0800

cyiannoulis gravatar image cyiannoulis
1201 10

Hi to all,

Consider the following simple zul:

<?page title="Welcome" contentType="text/html;charset=UTF-8"?>
<?init class="org.zkoss.zkplus.databind.AnnotateDataBinderInit" root="./winWelcome" ?>

<script type="text/javascript">
	
	function checkAttribute(element) {
		
		var component = document.getElementById(element.uuid);
		var attribute = component.getAttribute("MyAttribute");
		alert(attribute);
        }
	
</script>

<zk xmlns:w="http://www.zkoss.org/2005/zk/client" xmlns:d="http://www.zkoss.org/2005/zk/client/attribute">

    <window id="winWelcome" title="Welcome" border="normal" apply="controllers.WelcomeForm" width="450px" height="100%">

          <textbox id="txtLogin" w:onFocus="checkAttribute(this)" />

    </window>

</zk>

And the composer:

package controllers;

public class WelcomeForm extends GenericForwardComposer {

	private static final long serialVersionUID = 1L;
	
	/*
	 * The data binder 
	 */
	private AnnotateDataBinder binder;
	
	/*
	 * Autowired components
	 */
	private Window winWelcome;
	private Textbox txtLogin;
	
	public void doAfterCompose(Component window) {
		
		try {
			super.doAfterCompose(window);
		} 
		catch (Exception e) {
			e.printStackTrace();
		}
		
		if (self != null)
			self.setAttribute("controller", this);
		
		txtLogin.setAttribute("d:MyAttribute", "my-attribute");
	}
	
	public void onCreate$winWelcome(Event event) {
		
		binder = (AnnotateDataBinder) event.getTarget().getAttribute("binder", true);
		
		binder.loadAll();
	}
}

As you see what i am trying to do is to set the client-side attribute MyAttribute from the composer to the component txtLogin.
Unfortunately it doesn't work. Is there any other way to do this?

Thank you all

delete flag offensive retag edit

2 Replies

Sort by ยป oldest newest

answered 2011-05-05 02:18:47 +0800

cyiannoulis gravatar image cyiannoulis
1201 10

Cary, as you see i have declared the following namespace:

xmlns:d="http://www.zkoss.org/2005/zk/client/attribute"

I supposed that i had to set this attribute using the d prefix to declare it as a client-side attribute. Wrong. I found that it was quite easy to do this by using the following method:

txtLogin.setWidgetAttribute("MyAttribute", "my-attribute");

Thank you

link publish delete flag offensive edit

answered 2011-05-04 18:44:26 +0800

caclark gravatar image caclark
1753 2 5
http://clarktrips.intltwi...

txtLogin.setAttribute("d:MyAttribute", "my-attribute");

What's the "d:" for on the attribute name?

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-05-04 11:51:03 +0800

Seen: 399 times

Last updated: May 05 '11

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