Best Practice for Using EventBus with GWT Components

Let's say I have a C component that is used in two places in my application. For example, I have a list of folders that appear in 2 tabs. When a user renames a folder in one part, the change should be reflected in the other.

I am thinking to use EventBus for this and make a folder component to fire an EventBus event, say FolderChangedEvent. This event will be intercepted by the second component of the folder, which will automatically update its list of folders.

The question is, if I want to use this Folder component in 3rd place at some point, this location will also be updated. This may not be the desired behavior of the application.

Generally, I would make the Folder component my own addFolderChangedHandler (...) method, but since this component is quite complex and has subcomponents, this can create spaghetti-like code. However, I don't feel that this component should contain any application specific logic and send events directly to the EventBus application, which is too large for it.

What's the best practice for this case and how do you use EventBus effectively?


source to share

2 answers

The purpose of the event bus is to simplify event handling and make your code more readable and manageable. I will answer this question assuming your dilemma is how to classify events.

The GWTEvent class has a source () method. This speaks to who fired the event. If this is not acceptable, you can always have an "id" field in your event that is filled in by the sender. Therefore, if TreeView1 fires an event, the event ID will be set to "treeView1". When an event handler receives this event, it checks the identifier and decides to handle the event. This way, you can use one God event handler for your entire application.



EventBus is good for basic implementations, but I found in complex applications the need to send data using an event, such as a new NyObject that was created when the MyObjectCreated event was fired.

There is a good implementation of this in the GWT-Structs library . See There's an example on Dynamic Publisher Subscriber Binding to see how data can be passed with typed publishers and subscribers.



All Articles