-
FEATURED COMPONENTS
First time here? Check out the FAQ!
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.
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.)
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.
Asked: 2012-05-08 15:23:02 +0800
Seen: 160 times
Last updated: May 09 '12