Hi everyone!
While working on a project today I had an idea about a different way of binding event handlers to html elements. Partly inspired by angular 2, partly by React (or what I know of it, which is little enough).
And the idea kinda worked out and is neat and I’ll use it in my projects that are based on Blaze, I think, so I thought I’d share this and see what you guys (and gals) think.
EDIT: It’s now on atmosphere as themeteorites:blaze-magic-events! Check it out!
Copying the README here:
blaze-magic-events
A new way of binding event handlers to html elements for Meteor’s Blaze.
note: all code is ES6
Template
<template name="helloworld">
<button onclick={{sayHi}}>Say Hi!</button>
<button onclick={{reset}}>reset</button>
<p></p>
</template>
Event handlers
Template.helloworld.events({
sayHi (e, t) {
t.$('p').html('hi there from sayHi() handler!')
},
reset (e, t) {
t.$('p').html('')
},
})
Repo is on github, there’s some stuff hardcoded (like the helloworld template name reference) – EDIT: not any more! – , but it basically works and, as far as I can tell, should be universally usable. (It’s not a package yet – EDIT: now it is, see link above! --, but simply a POC put into an example app.)
Funny how we’re going back to the roots of using good old onclick
and friends
Cheers,
Denis
ps. Delegated event handling is great for many cases, too. I don’t think an idea such as this should replace all uses of the standard Blaze delegated event handling!
pps. Disclaimer: I’ve probably implemented things in such a way that a core Meteor dev would cry seeing it, but I just don’t know the Meteor internals well enough to understand which methods to call, who to ask basically for what I need… but it works and if there’s interest in collaborating and using it I’ll be happy to learn about the proper ways of implementing this and accept patches/PRs once it’s a proper package.