
Marko’s event API supports:
Note that you can’t mix event targets and event types: custom tags can only listen for custom events, and native tags can only listen for native events.
Both kinds of events are received with an on-* attribute and the attribute arguments syntax:
<input type="checkbox"on-change=event => console.info`Checked? `/>
input [type="checkbox"on-change=event => console.info`Checked? `)]
The first argument for the attribute can be a function, or a string matching a method name on the component’s class declaration.
If you provide a function as the first argument of the on-* attribute, the function is called whenever the event fires, like standard event listeners.
Below we use the static prefix to define a function, then use it as a click handler:
static<button on-clickhandleClick)>Log click</button>
staticbutton on-clickhandleClick) -- Log click
In the above example, any time the <button> is clicked the handleClick function is called.
You can also use an inline arrow function:
<button on-click => alert"Clicked! 🎉")>Celebrate click</button>
button on-click => alert"Clicked! 🎉") -- Celebrate click
…or anything that evaluates to a function:
const handler = input.dontBreakMyApp ?() => console.error("Clicked!") :() => { throw Error("Clicked!") }<button on-clickhandler)>Do not click</button>
When a string is the first argument, Marko calls a matching method on the component's class.
class<my-tabs on-switch-tab"logChange")>…</my-tabs>
classmy-tabs on-switch-tab"logChange") -- …
When <my-tabs> emits the switch-tab event, it will call its logChange method.
Within the handler you can access the current component instance, read data, emit events, update state, etc.
Arguments after the handler are prepended when the handler is called:
static<forfriend of=input.friends><button on-clickremoveFriend, friend.id)>Unfriend</button></for>
staticforfriend of=input.friendsbutton on-clickremoveFriend, friend.id) -- Unfriend
Here we share the logic for removeFriend() with each friend in the friends array. When the <button> is clicked, the id of the removed friend is passed to the removeFriend(), handler followed by the DOM click event.
The recommended way for a custom tag to communicate with its parent is through custom events.
All components implement a Node.js-style event emitter to send events to parent components.
class<input type="email" name=input.name on-change"handleChange")/>
classinput type="email" name=input.name on-change"handleChange")
The above code listens to native change events from the <input> element, and then emits its own email-change event if the change was valid.
<form><email-input name="email" on-email-change...)/></form>
formemail-input name="email" on-email-change...)
EDITNote: Events are not received as
input; you cannot accessinput.onEmailChange. Instead, they set up subscriptions.
Helpful? You can thank these awesome people! You can also edit this doc if you see any issues or want to improve it.