In the beginning of JS+DOM, it was a common practice to write HTML with DOM events (in JS): (`<button onclick="doSomething()">`). Server rendered most things anyway so you could (`<button onclick="toggle(<%= someId %>)">...<div id="item-<%= someId %>">...`).
Problem was tons of global functions all over the place, or many big objects and maintainability issues.
In the "jQuery era", you'd write your initial HTML in .html files and jQuery all the events. It brought infinite amount of ways to write code, which often led to many styles and no conventions.
<button class="show" data-text="hello">+</button>
$('.show').click(function () {
var $this = $(this)
var $div = $('<div>').text($this.data('text'))
$this.after($div)
})
etc.
Many problems with this approach:
- You can't tell which element has events bound to it just by looking at it. Any JS file can decide to do that
- Usage of a CSS class to find elements is just a hack
- Poorly named IDs/classes create collisions
- After an action, the initial HTML no longer represents the current state
- Server can render some HTML, then client changes it, and if things get out of sync, the only way to reset the state is to reload the entire page. Imagine a user signing in, and many parts of the page need to show things for a signed in user. Doing this manually would probably be error-prone, so it's just easier to reload the page and let the server do that.
And then came the modern front end libraries/frameworks like React, Angular, Ember etc.
The main difference is that now the HTML representation is ALWAYS in sync with the data, AND the functional code that's attached to each element is at the same place.
In the beginning of JS+DOM, it was a common practice to write HTML with DOM events (in JS): (`<button onclick="doSomething()">`). Server rendered most things anyway so you could (`<button onclick="toggle(<%= someId %>)">...<div id="item-<%= someId %>">...`). Problem was tons of global functions all over the place, or many big objects and maintainability issues.
In the "jQuery era", you'd write your initial HTML in .html files and jQuery all the events. It brought infinite amount of ways to write code, which often led to many styles and no conventions.
or or worse, etc.Many problems with this approach:
- You can't tell which element has events bound to it just by looking at it. Any JS file can decide to do that - Usage of a CSS class to find elements is just a hack - Poorly named IDs/classes create collisions - After an action, the initial HTML no longer represents the current state - Server can render some HTML, then client changes it, and if things get out of sync, the only way to reset the state is to reload the entire page. Imagine a user signing in, and many parts of the page need to show things for a signed in user. Doing this manually would probably be error-prone, so it's just easier to reload the page and let the server do that.
And then came the modern front end libraries/frameworks like React, Angular, Ember etc.
The main difference is that now the HTML representation is ALWAYS in sync with the data, AND the functional code that's attached to each element is at the same place.
And the jQuery example becomes this:
When you look at this piece of code you immediately understand the ways it can be rendered and what affects it. Super clear, no surprises.