Application hooks (the js-prefix)
mindplay-dk opened this issue · 1 comments
Thank you for this excellent reference! It's great to have this resource, and it's very well written and easy to understand.
There is one issue, which I find very controversial - your encouragement to use class names as application hooks.
A common practice is to use data-* attributes as JS hooks, but this is incorrect.
"incorrect" here is quite misleading - and the HTML5 spec makes it sound quite appropriate for exactly that:
Custom data attributes are intended to store custom data private to the page or application, for which there are no more appropriate attributes or elements
So, according to the spec, you'd have to be claiming that the class-attribute is "more appropriate" - but it is clearly designated for styling the visual appearance of elements, which Javascript hooks are not, and thereby clearly falls outside the defined meaning of the class-attribute.
data-* attributes, as per the spec, are used to store custom data private to the page or application (emphasis mine).
A Javascript hook is exactly that - it's data (in this case a flag) private to the page or application.
data-* attributes are designed to store data, not be bound to.
Again, I'm quoting the HTML 5 spec, which clearly states the exact opposite:
JavaScript libraries may use the custom data attributes, as they are considered to be part of the page on which they are used
It then goes on to provide more details and encouragement for library authors to make use of this feature.
The problem with using class-names, even with a js-
prefix, is it directly encourages the very thing you're trying to prevent: styling, which is the sole purpose of the class-attribute.
The data-attribute exists solely as a fallback attribute for application data, for which there is no more appropriate attribute, and the js-
prefix convention exists solely as a work-around to the same problem: the fact that the class-attribute is entirely inappropriate for Javascript hooks.
I urge you to reconsider your point of view.
No comment? :-)