πŸ”₯Β Β Hot Reload for JS and CSS now available!Β With Zermatt v1.2.0.Β Read more.

Docs
Pub/sub with Zermatt.Event

Pub/sub with Zermatt.Event

Using Alpine.bind to attach Zermatt components to existing DOM elements.

See feature documentation.

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