ZK - Open Source Ajax Java FrameworkZK - Open Source Ajax Java Framework

how to create a custom event

gmitev
6 May 2009 06:54:45 GMT
6 May 2009 06:54:45 GMT

I would like to notify a controller that some data in another controller has changed, however I do wish the two controllers to know about each other.

I was thinking that I could do somthing like:-

Controller A:
Events.postEvent(new Event(MY_EVENT, self, null));

Controller B:
self.addEventListener(MY_EVENT, this);

But I get an error:

Invalid event name: MY_EVENT

Do i need to register my event somewhere/somehow???

Hari
6 May 2009 11:48:28 GMT
6 May 2009 11:48:28 GMT

hai gmitev,
u just simply use this method.........
events.addForward("onClick", "self", "onClickAdd");

gmitev
7 May 2009 01:24:59 GMT
7 May 2009 01:24:59 GMT

hi hari,
there is no such method as addForward in Events class?

I have tried putting,
comp.addEventListener(Constants.SHOPPING_BASKET_UPDATE_EVENT, this);

in doAfterCompose() method of the controller that is the listener and I have overriden onEvent() method, other events are getting in there but not my custom event.

From the calling controller, I do

Events.sendEvent(new Event(Constants.SHOPPING_BASKET_UPDATE_EVENT, self));

What am I missing? Still no luck.

I dont want to hard code the call to the other controller as this is NOT elegant, and if more controllers wish to listen for this event this seems the best way, if it can work.

dennis
7 May 2009 02:27:30 GMT
7 May 2009 02:27:30 GMT

what is "self" in this case, is it same instance of "this"
provide a reproducible code, don't let everybody guess your code.

gmitev
7 May 2009 02:43:13 GMT
7 May 2009 02:43:13 GMT

i have provided the code, those are the lines of interest, and yes self is same instance of "this".

When something happens in one controller, i wish to notify all listeners (controllers) of the change.

Sounds simple enough, but how?

dennis
7 May 2009 02:47:58 GMT
7 May 2009 02:47:58 GMT

yes, simple enough and isn't reproducible , isn't ?

gmitev
8 May 2009 00:41:13 GMT
8 May 2009 00:41:13 GMT

dennis, what do you mean? isn't reproducible?

In general how would you notify other controllers of an event without calling them directly?

dennis
8 May 2009 01:57:38 GMT
8 May 2009 01:57:38 GMT

Who knows what happened if I only got 2 line code?
I am not saying this isn't a bug of ZK,
But, who knows which part did the mistake? maybe ZK bug, maybe you did some thing wrong.
There are too many side effects in any program isn't it?

I am trying to help you, but ....I don't want to GUESS what could cause this.
Help me is helping youself if you can provide a reproducible code, not just 2 line code in different controller.

jdee
9 Feb 2012 01:14:09 GMT
9 Feb 2012 01:14:09 GMT

I ran into the same problem, and after looking at the source code I learned that the event name must simply begin with "on" and the third character must be upper case.
So for example, use a constant like:
final static String MY_EVENT = "onMyEvent";
and then
someComponent.addEventListener(MY_EVENT, new MyEventListener());
works just fine at runtime.

I use this model to synchronize Panels and Windows that need to show consistent data, like master-details, etc.

Matze2
9 Feb 2012 14:45:50 GMT
9 Feb 2012 14:45:50 GMT

Very good catch, jdee. The error is raised in AbstractComponent.addEventListener, which does exactly the above mentioned check.
If we would have had the stacktrace, it would have been only two posts in this thread.

Here is the check implementation:

	public static final boolean isValid(String name) {
		return name != null && name.length() > 2
			&& name.charAt(0) == 'o' && name.charAt(1) == 'n'
			&& Character.isUpperCase(name.charAt(2));
	}