ultraq / thymeleafjs

A basic implementation of the Thymeleaf templating engine in JavaScript
Apache License 2.0
52 stars 8 forks source link

Applying event listeners #32

Open ultraq opened 5 years ago

ultraq commented 5 years ago

When using ThymeleafJS on the browser, I'm doing a lot of setting of event handlers after the template has been rendered. It'd be cool if there was some way to automatically apply event handlers on the template after the rendering process. eg:

Taking inspiration from Vue:

new Vue({
  el: 'selector-to-vue-template',
  methods: {
    // Methods here which are mapped from the template via directives like v-on:click="methodName"
  }
});

And in React using JSX:

<button onclick={/* JS function here */}>Click me!</button>

So for Thymeleaf, it might look something like:

templateEngine.process('<button th:onclick="methodName">Click me!</button>', {
  methods: {
    methodName: event => {
      // ...
    }
  }
});

Some things that come to mind:

🤔