0

Creating custom components

asked 2008-10-03 09:33:21 +0800

guleri gravatar image guleri
75 2

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

delete flag offensive retag edit

4 Replies

Sort by ยป oldest newest

answered 2008-10-03 23:05:14 +0800

guleri gravatar image guleri
75 2

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()...

link publish delete flag offensive edit

answered 2008-10-06 03:47:57 +0800

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

updated 2008-10-06 03:52:43 +0800

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.
};

link publish delete flag offensive edit

answered 2010-07-19 11:28:26 +0800

ncmartin gravatar image ncmartin
9

Hi, in 5.0.3, exist Componentcommand?

link publish delete flag offensive edit

answered 2010-07-23 10:34:16 +0800

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

No, use service() instead. please see the article

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: 2008-10-03 09:33:21 +0800

Seen: 501 times

Last updated: Jul 23 '10

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