-
FEATURED COMPONENTS
First time here? Check out the FAQ!
Hi I try to build my own component, but have a little problem firing events back to the server...
My XulElement component..
public class FlightMenu extends XulElement { private int totalItemCount = 0; private static Command collapsAll = new ComponentCommand("onCollapsAll", 0) { @Override protected void process(AuRequest request) { final FlightMenu fmenu = (FlightMenu)request.getComponent(); fmenu.doCollapsAll(); Events.postEvent(new Event(getId(), fmenu, request.getData())); } }; @Override public Command getCommand(String cmdId) { if("collapsAll".equals(cmdId)) { return collapsAll; } return super.getCommand(cmdId); } void doCollapsAll() { //Do some stuff... System.out.println("I really want to get into this method!!! :-/ "); //Response back to browser.. Not really sure how these values should map.. response("ctrl", new AuInvoke(this, "collapsAll", "test123")); this.invalidate(); //is this needed? } @Override public String getOuterAttrs() { // Not sure exactly what i am doing in her to be honest.. O:) . what must be in place. (what about sub html elements?) StringBuffer sb = new StringBuffer(); sb.append(Events.isListened(this, "onCollapsAll", true) ? " z.onCollapsAll=\"true\"" : " "); sb.append(" " + super.getOuterAttrs()); return sb.toString(); } }
My Javascript
zkFlightMenu = {}; zkFlightMenu.init = function(cmp) { var collapsAll = $e(cmp.id + "!CollapsAll"); collapsAll.onclick = zkFlightMenu.onCollapsAll; zk.listen(cmp, "onCollapsAll", zkFlightMenu.onCollapsAll); //not sure about this.. }; zkFlightMenu.onCollapsAll = function(evt) { if(!evt) { evt = window.event; } var target = Event.element(evt); var parentId = target.id.substr(0, idStr.indexOf("!")); zkau.sendasap({uuid: parentId , cmd: 'onCollapsAll', ctl: true}, zkau.asapTimeout($e(parentId),'onCollapsAll')); alert("sendt..??"); //Well i really do get into this javascript menthod but noting on the server side. // Nope! This is not working and nothing is triggered on the server ;(... }; //From server to client zkFlightMenu.setAttr = function (cmp, name, value) { alert("cmp:" + cmp + "\nname:" + name + "\nvalue:" + value); if("disabled" == name) { zkau.setAttr(cmp, name, value); //update cmp.disabled first //whatever return true; //attribute has been updated } return false; };
And my mold/renderer code is like this.. (no dsp, i need the very best performance..)
public class FlightMenuDefaultView implements ComponentRenderer { @Override public void render(Component comp, Writer out) throws IOException { final FlightMenu self = (FlightMenu)comp; out.write("<div id=\""); out.write(self.getUuid()); out.write("\" z.type=\"path.menu.FlightMenu\""); out.write(self.getOuterAttrs()); out.write(self.getInnerAttrs()); out.write(" >"); out.write("<a id=\"" + self.getUuid() + "!CollapsAll\" >"); out.write(this.getCollapsLabel()); out.write("</a>"); // And so on... } }
As u can see here i have an element under that i want to triggering the event.
(Do i need to only build upon other components?)
As a summary i want to trigger an event from my javascript method zkFlightMenu.onCollapsAll to the server side void doCollapsAll()..
Any thoughts? =) (or obvious errors in my code..)
Thanks!
-Erik
Something got firing finally!
getOuterAttrs()'s sb.append(Events.isListened() is of course returning false for a custom event like this..
zk.listen(collapsAll, "click", zkFlightMenu.onCollapsAll); is also the one to use in init()...
Hi,
Here are my answers:
-->sb.append(Events.isListened(this, "onCollapsAll", true) ? " z.onCollapsAll=\"true\"" : " ");
Do this.
appendAsapAttr(sb, "onCollapsAll"); // shorthand
-->zk.listen(cmp, "onCollapsAll", zkFlightMenu.onCollapsAll); //not sure about this..
zk.listen() function is used for regular browser's event, like click, mouseover, mouseout, and so on.
For example,
zkFlightMenu.init = function(cmp) { var collapsAll = $e(cmp.id + "!CollapsAll"); //collapsAll.onclick = zkFlightMenu.onCollapsAll; // DO this. zk.listen(collapsAll, "click", zkFlightMenu.onCollapsAll); }; zkFlightMenu.onCollapsAll = function(evt) { alert("This button have been pressed!"); var target = Event.element(evt); var cmp = $outer(target); // returns the enclosing element (not ends with '!') zkau.sendasap({uuid: cmp.id, cmd: "onCollapsAll", data: [if-any]}) // This event only fire when onCollapsAll has been registered on this component, // otherwise, that means developer doesn't care this event, so it will be fired with other ASAP event together. };
No, use service() instead. please see the article
Asked: 2008-10-03 09:33:21 +0800
Seen: 501 times
Last updated: Jul 23 '10