Skip to content

Commit f17a10d

Browse files
Merge pull request #891 from liferay/bryceosterhaus-patch-2
chore(guidelines): add full example for event bus
2 parents 2cefee2 + d81d547 commit f17a10d

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

guidelines/dxp/liferay_global_event_bus.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,27 @@ Liferay.fire('someEvent', {
127127
});
128128
```
129129

130+
## Full Example
131+
Let's say you have two modules on the same page, A & B. In module A, you have the following code in your Javascript.
132+
133+
```jsx
134+
// Module A
135+
Liferay.on('my-custom-event', (data) => {
136+
alert(data);
137+
});
138+
```
139+
140+
And then, in module B, we render a button with the code below
141+
142+
```jsx
143+
// JSX in Module B
144+
<button onClick={() => Liferay.fire('my-custom-event', 'Hello!')}>
145+
Say Hello!
146+
</button>
147+
```
148+
149+
By clicking the button in Module B, the event will trigger in Module A and execute `alert('Hello!')`
150+
130151
## Best Practices
131152

132153
The best practice number #1 is using Liferay's namespace utilities (`Liferay.Util.ns`) when possible. Like `Liferay.Util.ns(myWidgetNamespace, 'nameOfMyCustomEvent')`. For scoping the global name of the event. It will prevent you from listening to generic events which can be fired in different widgets for which you may not be responsible.

0 commit comments

Comments
 (0)