-
FEATURED COMPONENTS
First time here? Check out the FAQ!
I have a simple javascript function defined in my .zul file :
<script type="text/javascript"> var doBeep = false; function setBeep(beepVar) { doBeep = beepVar; } </script>
When I added the following directly into the .zul - it works fine :
<image id="_audioHome" sclass="dhAudioHomeIcon" xmlns:w="http://www.zkoss.org/2005/zk/client" w:onMouseDown="setBeep(true)"/>
What I need to do, is inside my java class, dynamically add that call - it can't be hardcoded into the zul since the components that need to make the call are created programatically.
I tried the following but that doesn't work - I get scripting errors and it didn't seem to execute the onMouseDown either.
private void addZscript() { ZScript zscript = new ZScript("javascript","setBeep(true);"); EventHandler handler = new EventHandler(zscript); _audioHome.addEventHandler("onMouseDown", handler); }
Does anyone know how to do this?
I would need to add the
onMouseDown="setBeep(true)"
Andy,
I don't believe onMouseDown is a regular event that ZK supports at this point.
If you can live with the onClick event instead, then I have a couple of alternatives you might think about. Consider the following zul (which is pointing to a controller in the MVC fasion):
<zk> <script type="text/javascript"> var doBeep = false; function setBeep(beepVar){ doBeep = beepVar; } </script> <window id="main" apply="MyController"> </window> </zk>
The first controller alternative uses Server-side events that call Javascript:
import org.zkoss.zk.ui.util.GenericForwardComposer; public class MyController extends GenericForwardComposer { Window main; public void doAfterCompose(Component comp) throws Exception { super.doAfterCompose(comp); addImage(); addImage(); addImage(); } public void addImage(){ Image img = new Image(); img.setSrc("http://www.zkoss.org/forum/images/home.png"); main.appendChild(img); Separator separator = new Separator(); separator.setHeight("20px"); main.appendChild(separator); img.addEventListener("onClick", new EventListener() { public void onEvent(Event event) throws Exception { Clients.evalJavaScript("setBeep(true)"); } }); } }
The other controller alternative uses a jQuery selector to bind an onClick event to all the images.
import org.zkoss.zk.ui.util.GenericForwardComposer; public class MyController extends GenericForwardComposer { Window main; public void doAfterCompose(Component comp) throws Exception { super.doAfterCompose(comp); addImage(); addImage(); addImage(); Clients.evalJavaScript("jq(\"@image\").click(function(){setBeep(true);})"); } public void addImage(){ Image img = new Image(); img.setSrc("http://www.zkoss.org/forum/images/home.png"); main.appendChild(img); Separator separator = new Separator(); separator.setHeight("20px"); main.appendChild(separator); } }
Maybe that will help.
Regards,
Todd
Hi,
Another solution is add a client (widget ) listener, please refer to http://books.zkoss.org/wiki/ZK_Client-side_Reference/General_Control/Event_Listening#Declare_a_Client-side_Listener_in_ZUML
In composer you can call
image.setWidgetListener("onClick", "setBeep(true);");
Thanks so much for the replies. I will try them but I need to use "onMouseDown".
The reason for this is that I am calling my webpage from an IOS App, I intercept the user tap on the webpage (on an ipad), and then in the ios callback I send a javascript call over to my webpage to ask for a variables value (doBeep). If it's true I make a "click" sound, if it's false I don't.
If I try and use onClick in my zk code, it happens after the app has already called over asking for the javascript variable - which is too late.
If I use onMouseDown in my zk code, it does get executed first, so the variable is set properly when my IOS app calls over and asks.
Thanks!
Asked: 2012-04-13 13:27:53 +0800
Seen: 1,022 times
Last updated: May 31 '12