Gurigraphics / DOMinus.js

DOMinus.js is a reactive data binding library that turn HTML irrelevant.
4 stars 1 forks source link

Event as function instead of string #20

Closed thipages closed 5 years ago

thipages commented 5 years ago

Hi, Could be interesting to move from HTML.element={html='text', onclick:"update()"}; to HTML.element={html='text', onclick: ()=> update() };

A quick and dirty implementation could be to identify the two first character property starting with on

Gurigraphics commented 5 years ago

Why would it be better than that?

<button onclick="update()">Click me</button>
thipages commented 5 years ago

From a result point of view , nothing. The point is more for development

Gurigraphics commented 5 years ago

I think that is only more code. onclick: ()=> update() onkeypress: ()=> update() onmouseover: ()=> update() etc

thipages commented 5 years ago

Two advantages

  1. Replace hard coding string => good for IDEs and code check
  2. It allows to do in one line (a bit like style vs class) :
    HTML.element={html='text', onclick: ()=> {/* doSomething */ };
Gurigraphics commented 5 years ago

But this code is not mounting the HTML:

HTML.element={ 
  html='text', 
  onclick: ()=> {/* doSomething()*/ }
};
<button onclick="HTML.element.onclick()">Click me</button>

I prefer to keep this in separate module.

HTML.element={ 
  html='text', 
  onclick: "EVENT.element.click()"
};
<button onclick="EVENT.element.update()">Click me</button>
EVENT.element.click= function(){

}

Others prefer use "addEventListener". Mix events with HTML is like Vue.

thipages commented 5 years ago

For completion, Function may be converted to Json and then evaluated https://stackoverflow.com/questions/12651977/why-cant-you-stringify-a-function-expression https://github.com/vkiryukhin/jsonfn/blob/master/jsonfn.js