Pub/sub with Zermatt.Event
Using Alpine.bind to attach Zermatt components to existing DOM elements.
Below is a complete example of an advanced use of Zermatt.Event
.
Open these files from the /vendor/maddlen/module-zermatt-examples/view/frontend
directory:
templates/events.phtml
web/js/zermatt/modules/events.js
The events.phtml
template has a button which triggers the onInputChange()
method of events.js
.
Zermatt.Event.on
Without changing anything in the events.js
file here is how the browser console looks like when hitting the "Trigger" button 6 times.
π onInputChange
π Count: 1
π’ event-1 received
Completed events: ['event-1'] ['{"message":"Event 1 dispatched"}']
Completed events: ['event-2'] ['{"message":"Event 2 dispatched"}']
π onInputChange
π Count: 2
π’ event-1 received
Completed events: ['event-1'] ['{"message":"Event 1 dispatched"}']
Completed events: ['event-2'] ['{"message":"Event 2 dispatched"}']
π onInputChange
π Count: 3
π’ event-1 received
Completed events: ['event-2'] ['{"message":"Event 2 dispatched"}']
π onInputChange
π Count: 4
π’ event-1 received
Completed events: ['event-2'] ['{"message":"Event 2 dispatched"}']
π onInputChange
π Count: 5
π’ event-1 received
π onInputChange
π Count: 6
π’ event-1 received
Zermatt.Event.once
If this.subscriber = Zermatt.Event.on
becomes this.subscriber = Zermatt.Event.once
, the browser console now shows:
π onInputChange
π Count: 1
π’ event-1 received
Completed events: ['event-1'] ['{"message":"Event 1 dispatched"}']
Completed events: (2)Β ['event-1', 'event-2'] (2)Β ['{"message":"Event 1 dispatched"}', '{"message":"Event 2 dispatched"}']
π onInputChange
π Count: 2
π’ event-1 received
π onInputChange
π Count: 3
π’ event-1 received
π onInputChange
π Count: 4
π’ event-1 received
π onInputChange
π Count: 5
π’ event-1 received
π onInputChange
π Count: 6
π’ event-1 received
Zermatt.Event.waitOn
If this.subscriber = Zermatt.Event.on
becomes this.subscriber = Zermatt.Event.waitOn
, the browser console now shows:
π onInputChange
π Count: 1
π’ event-1 received
Completed events: (2)Β ['event-1', 'event-2'] (2)Β ['{"message":"Event 1 dispatched"}', '{"message":"Event 2 dispatched"}']
π onInputChange
π Count: 2
π’ event-1 received
Completed events: (2)Β ['event-1', 'event-2'] (2)Β ['{"message":"Event 1 dispatched"}', '{"message":"Event 2 dispatched"}']
π onInputChange
π Count: 3
π’ event-1 received
Completed events: ['event-2'] ['{"message":"Event 2 dispatched"}']
π onInputChange
π Count: 4
π’ event-1 received
Completed events: ['event-2'] ['{"message":"Event 2 dispatched"}']
π onInputChange
π Count: 5
π’ event-1 received
π onInputChange
π Count: 6
π’ event-1 received
Zermatt.Event.waitOnce
If this.subscriber = Zermatt.Event.on
becomes this.subscriber = Zermatt.Event.waitOnce
, the browser console now shows:
π onInputChange
π Count: 1
π’ event-1 received
Completed events: (2)Β ['event-1', 'event-2'] (2)Β ['{"message":"Event 1 dispatched"}', '{"message":"Event 2 dispatched"}']
π onInputChange
π Count: 2
π’ event-1 received
π onInputChange
π Count: 3
π’ event-1 received
π onInputChange
π Count: 4
π’ event-1 received
π onInputChange
π Count: 5
π’ event-1 received
π onInputChange
π Count: 6
π’ event-1 received