0

ZK5 - Best practice for EventListeners - notifying back to controller? callbacks?

asked 2012-05-08 15:23:02 +0800

rickcr gravatar image rickcr
704 7

I apologize if this is an obvious question, but I'm trying to figure out best practices and right now the docs when I'm searching have a hybrid between doing a lot of stuff within zkscript in the zk pages and then I find later docs showing the nice zk6 annotations on controller methods.

I've inherited and existing zk5 application and I'm trying to keep the UI clean and having all the java stuff in the controllers or separate components.

What I'm most curious about is I'm currently add some event listeners to components after onCompose in my controller like:

Button importButton = (Button)self.getFellow("importFile");
importButton.addEventListener("onUpload", new UploadUpcListener(upcList));

Once my UploadUpcListener fires though and completes, what's the best way to notify back to the controller of things that have completed? Notice I passed the upcList to the listener... should I pass a bunch of other things as well? Or is there a convention of sending a notification event back to the controller that it listens to? I don't see many examples illustrating this kind of stuff so maybe I'm thinking of things in the wrong way.

Let's just take the scenario above. After onUpload completes and the upcList is modified after the upload, how is the controller best notified things are completed so it can display a message "Upload complete. You updated 5 items." Passing a bunch of components to the Listener sounds like a bad idea so I'm thinking a callback is the best approach but not sure how to best handle that pattern with ZK.

Thanks for any info.

delete flag offensive retag edit

3 Replies

Sort by ยป oldest newest

answered 2012-05-08 20:50:54 +0800

rickcr gravatar image rickcr
704 7

The cheesy way I'm doing this now is passing the instance of the controller to the EventListener so I can call some other controller method when the eventListener has finished processing. Clearly this is really lame, but I'm not sure of the approach to take. In traditional event-driven guis, I could fire off a custom event "HandlingOfUPCuploadComplete" and then any registered listeners to this event would be notified (in this case the controller.)

link publish delete flag offensive edit

answered 2012-05-09 06:18:45 +0800

Matze2 gravatar image Matze2
773 7

I think, that this approach - sending an event to the controller's component - is quite popular. In this case, you need the component and not the controller itself.
Other alternative: you pass the controller and invoke a method directly (maybe more "cheesy").

If you do the event approach, then do not forget to respect the naming convention for events: it has to Start with "on", third character is uppercase.

Note, that I didn't fully understood the context of your problem. I never had to use a single addEventListener() call in my code base.
In my case, "onUpload" is directly fired on the composer's component, so I could implement the handling directly in composer without headaches.

link publish delete flag offensive edit

answered 2012-05-09 06:23:25 +0800

Matze2 gravatar image Matze2
773 7

Saw your second thread about the same topic too late. Seems you have solved it on your own.

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: 2012-05-08 15:23:02 +0800

Seen: 160 times

Last updated: May 09 '12

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