DEV Community

Arhan Khan
Arhan Khan

Posted on

Backbone.js for Beginners: The Tiny Framework That Taught the Web How to Organise Code

Backbone.js is one of the oldest JavaScript frameworks still around. It is small — a few kilobytes — and it does only three things: models, views, and events. Modern frameworks do far more, but Backbone is worth learning because it shows you the core ideas underneath all of them, with nothing hidden.

The three pieces

Models hold your data and the rules around it. Collections are ordered groups of models. Views render the data and listen for user actions. Events tie it all together — when a model changes, any view listening to it updates.

// A model: one todo item
const Todo = Backbone.Model.extend({
  defaults: {
    title: "",
    done: false,
  },
  toggle() {
    this.set("done", !this.get("done"));
  },
});

// A collection: the list of todos
const TodoList = Backbone.Collection.extend({
  model: Todo,
});

const todos = new TodoList();
Enter fullscreen mode Exit fullscreen mode

Views and events

A view listens to its model and re-renders when the model changes. No virtual DOM, no framework magic — just a render function that runs when data changes:

const TodoView = Backbone.View.extend({
  tagName: "li",

  events: {
    "click .toggle": "onToggle",
  },

  initialize() {
    this.listenTo(this.model, "change", this.render);
  },

  onToggle() {
    this.model.toggle();
  },

  render() {
    this.$el.html(
      `<input type="checkbox" class="toggle" ${this.model.get("done") ? "checked" : ""}>
       ${this.model.get("title")}`
    );
    return this;
  },
});
Enter fullscreen mode Exit fullscreen mode

That listenTo line is the whole idea: the view says "tell me when this model changes", and Backbone handles the rest. React's state updates and Vue's reactivity are the same idea with more machinery.

Routing without a framework

Backbone has a small router that maps URL fragments to functions, so a todo app can have shareable URLs like #/todos/5:

const Router = Backbone.Router.extend({
  routes: {
    "": "showAll",
    "todos/:id": "showOne",
  },
  showAll() {
    console.log("Showing all todos");
  },
  showOne(id) {
    console.log("Showing todo", id);
  },
});

new Router();
Backbone.history.start();
Enter fullscreen mode Exit fullscreen mode

The honest trade-offs

Backbone gives you structure but almost no conveniences. There is no built-in templating, no data binding in the HTML, and no component system — you write the render logic yourself, and big apps end up with a lot of boilerplate. The ecosystem is quiet now: most new projects choose React, Vue, or Svelte, and hiring for Backbone is rare.

Why it is still worth learning

Because it is small enough to read in an afternoon. You can open the source, understand every line, and see exactly how models, events, and views work. That understanding transfers directly to every framework you learn next — and when you debug a React app, you will recognise the same patterns wearing different clothes.

Top comments (0)