Skip to content
FrontDepth, Home

jQuery → Component frameworks (Vue, Angular, Svelte)

2006 – today · 2014 – today

Older era
Newer era
Compare

What changed

Where the state lives

jQuery

In the DOM: a todo is done when its <li> has the class done, and the counter is found by counting elements.

Component frameworks (Vue, Angular, Svelte)

In the component's data(): plain objects and arrays that Vue makes reactive. shown and left are computed, cached until the data they read changes.

How the page is rendered

jQuery

The server sends the first HTML. New items are built with $("<li>") and appended.

Component frameworks (Vue, Angular, Svelte)

Vue compiles the HTML template (written in the page here) into a render function. When data it read changes, the component re-renders and Vue patches only what differs.

What happens on a change

jQuery

Each handler changes the elements it knows about, then refresh() fixes the counter and the filter.

Component frameworks (Vue, Angular, Svelte)

Methods and v-model change the data directly (this.todos.push, todo.done); Vue buffers the DOM update until the next tick, a microtask.

How the code is organised

jQuery

One script of event handlers. A delegated handler on #todo-list also covers items added later.

Component frameworks (Vue, Angular, Svelte)

One component object of options (data, computed, methods) plus its template. Larger apps nest such components into a tree.

Why it changed

What hurt in each era along the way, and the era that came next.

  1. jQuery→MVC in the browser (Backbone)

    When the data lives only in the DOM, every feature reads and writes the page directly: the counter counts <li> elements, the filter checks classes. As apps grew, handlers changing the page from many places became hard to follow ("jQuery spaghetti"), and there was no model of the data to test, save or reuse. Libraries like Backbone moved the data into models and kept the page in views.

  2. MVC in the browser (Backbone)→Two-way binding (AngularJS)

    Backbone left the wiring to you. Every view subscribed to its models by hand, chose what to redraw and read form fields back itself, so much of the code only connected data to the DOM. A view thrown away without remove() stayed subscribed and kept reacting as a "zombie view", and re-rendering a template replaced elements, losing focus and half-typed text. AngularJS answered with two-way data binding: the HTML declares what shows what, and the framework keeps the page and the data in sync.

  3. Two-way binding (AngularJS)→React and the virtual DOM

    Every binding added a watcher, and every digest checked all of them, at least twice whenever something had changed, so pages with many bindings slowed down on each click or keystroke (1.3 added one-time bindings, ::, to cut the count). Two-way bindings and watchers that change other values made it hard to see what changed what, and code outside AngularJS, like a setTimeout callback or a jQuery plugin, had to remember to call $scope.$apply().

    Angular 2 was a new framework rather than the next version: an app could not just upgrade, and the official path was to migrate it piece by piece, with both frameworks running side by side. React answered with one-way data flow: a component renders from its state, and a virtual DOM works out what to change on the page.

  4. React and the virtual DOM→Flux and Redux

    State that several components need has to live in their closest common parent and travel down as props through every component in between, even those that only pass it on, while callbacks travel down the same way to change it. In a large app, setState calls in many components, triggered by clicks, server responses and timers, all changed what the screen showed, so it was hard to follow why it looked the way it did. Flux and Redux moved the shared state out of the components into a store that changes only through actions.

  5. Flux and Redux→Component frameworks (Vue, Angular, Svelte)

    Every small feature needed the same pieces in several files: an action type constant, an action creator, a case in a reducer, and mapStateToProps / mapDispatchToProps to connect a component, plus objects copied by hand so that nothing was mutated. Setting up a store with middleware and the DevTools took more code still. Redux itself later answered with Redux Toolkit; component frameworks like Vue, Angular and Svelte offered reactive state you change directly and HTML templates that update themselves, with much less ceremony.

The same app, side by side

The main file of each version. Both apps pass the same test: same markup, same behaviour, different ideas.

The apps run in sandboxes in your browser.

jQuery
Component frameworks (Vue, Angular, Svelte)

jQuery

jQuery 4.0.0

/src/main.js · 52 lines

JavaScript
// jQuery era: the page itself is the state. Handlers read the DOM, change it, recount.
import $ from "jquery";

// $(fn) waits until the document is ready, like $(document).ready(fn).
$(function () {
  let filter = "all";

  // Brings the counter, the visible items and the filter buttons in line with the DOM.
  function refresh() {
    const left = $("#todo-list li").not(".done").length;
    $("#todo-count").text(left + " left");

    $("#todo-list li").each(function () {
      const done = $(this).hasClass("done");
      $(this).toggle(filter === "all" || (filter === "done") === done);
    });

    $("[data-filter]").removeClass("selected");
    $('[data-filter="' + filter + '"]').addClass("selected");
  }

  $("#todo-form").on("submit", function (event) {
    event.preventDefault();
    const title = String($("#new-todo").val()).trim();
    if (!title) return;

    // Build the new <li> the same way the server wrote the first ones.
    const $item = $("<li>").append(
      $("<label>").append(
        $('<input type="checkbox" data-toggle>'),
        " ",
        $('<span class="title">').text(title),
      ),
    );
    $("#todo-list").append($item);
    $("#new-todo").val("");
    refresh();
  });

  // One delegated handler on the list also covers the items added later.
  $("#todo-list").on("change", "[data-toggle]", function () {
    $(this).closest("li").toggleClass("done", this.checked);
    refresh();
  });

  $(".filters").on("click", "[data-filter]", function () {
    filter = $(this).data("filter");
    refresh();
  });

  refresh();
});

Component frameworks (Vue, Angular, Svelte)

Vue 3.5.45

/src/main.js · 41 lines

JavaScript
// Component-framework era (Vue): the component declares its state, derived values and
// methods; the HTML template in index.html describes the page. Vue keeps the two in sync.
import { createApp } from "vue";

let nextId = 3;

createApp({
  // data() returns the state. Vue makes it reactive: while rendering it records which
  // properties were read, and any later change to them re-renders the component.
  data() {
    return {
      todos: [
        { id: 1, title: "Read the docs", done: true },
        { id: 2, title: "Write the app", done: false },
      ],
      filter: "all",
      title: "",
    };
  },

  // Computed values are cached and re-evaluated only when the data they read changes.
  computed: {
    shown() {
      return this.todos.filter((t) => this.filter === "all" || (this.filter === "done") === t.done);
    },
    left() {
      return this.todos.filter((t) => !t.done).length;
    },
  },

  methods: {
    addTodo() {
      const title = this.title.trim();
      if (!title) return;
      // A plain push is enough: no new array, no setState. Vue sees the change itself.
      this.todos.push({ id: nextId++, title, done: false });
      this.title = "";
    },
  },
  // There is no template option, so mount() uses the HTML inside #app as the template.
}).mount("#app");