Most of the attention that JavaScript gets is all about creating large, monolithic Single Page Applications (SPAs). But the reality is that a great percentage of websites still use much simpler jQuery and vanilla JavaScript. Without going all-in on moving everything to a SPA, can you gain some of the benefits of using a framework to simplify your code and make it more reliable and testable? Sure, you can.

In many cases, moving to a SPA framework means a complete re-thinking of your application. It’s a change in how you approach building applications. I whole-heartedly recommend that you think about it this way if you’re building new applications, as it can really change the way you approach Web development, but…

In many cases, it‘s beneficial to ramp up to these technologies. Tearing down your jQuery empire and adding something like Angular, Vue, or React is a big leap. That’s one of the reasons I love how Vue works.

In this article, my goal is to give you, the jQuery user, a taste of the different approach that Vue takes and how this can improve your code, your markup, and your ability to build apps quickly. Yeah, quickly.

What’s Wrong with jQuery?

Nothing’s wrong with jQuery. Really, nothing. It’s been pivotal to creating most of the great websites you know. jQuery was responsible for making cross-browser/cross-OS websites work. It’s great.

But in many ways, it’s getting long in the tooth. Let’s take a quick example from Jake Rocheleau’s blog post here: https://speckyboy.com/building-simple-reddit-api-webapp-using-jquery/. He created a small example that uses jQuery to show how to call the GitHub API. You can see it in Figure 1:

Figure 1: Simple jQuery Page

The code is simple but shows a lot of the benefits and drawbacks of jQuery. Let’s break it down. You can see the JavaScript in Listing 1. Most of the code is inside one large event handler:

  $('#ghsubmitbtn').on('click', function(e){
    e.preventDefault();
    ...

Although this is pretty easy to remedy, it’s a common practice because it’s easy to think of an event handler as the main place for code in jQuery.

Next up is changing the UI in jQuery:

    $('#ghapidata').html(`
    <div id="loader">
      <img src="https://i.imgur.com/UqLN6nl.gif"
           alt="loading...">
    </div>`);

It starts up immediately with code that builds up markup in code. This mixes the metaphors of UI and logic into a single file. Getting the markup correct with in-line text is notoriously fragile because there is no real syntax checking.

Next, the code using jQuery to read a value from an input:

    var username = $('#ghusername').val();

Although this is straightforward, it requires you to use an ID on every input that you need to get data out of. It also means a query over the entire DOM which can be slow, even at the fast speeds of jQuery. But I suspect if you’re reading this article, you already know about the issues with jQuery. How can you make it better with Vue?

Using Vue in Place of jQuery

I think the best way to describe the way that Vue works is to convert this simple jQuery app. You can see in Listing 1 the complete JavaScript of the project and Listing 2 contains the complete Markup. Let’s rebuild this piece by piece so you can see how Vue can be used to do this easier and with less code.

Creating the Vue Object

One of the things I really like about Vue is that instead of requiring you buy into a big ecosystem, you canjust drop a JavaScript file and start working with it. To get started, you’ll just use a link to a development version of the library:

<!-- development version,
     includes helpful console warnings -->
<script
  src="//cdn.jsdelivr.net/npm/vue/dist/vue.js">
</script>

In fact, in this example, I left the jQuery in the project as I’ll get back to using it a little later. Yeah, really.

The basis of how Vue works is a Vue object. I just created a new JavaScript file and started out with just an instance of the Vue object:

var app = new Vue({
  el: "#w"
});

This object takes a JavaScript object to specify options. The first part that’s important is the el property. That represents a selector for the parent element in the HTML that you’re telling Vue to take over for. This is a key difference between jQuery and Vue. The magic of jQuery is to be able to search through the DOM and find the elements you’re interested in. Vue takes responsibility for a section of the DOM (or the entire DOM). So, in this case, you want set up the code to take over the element called w:

<body>
  <div id="w" class="container">
    <h1>Simple Github API Webapp</h1>
    <p>
      Enter a single Github username below and
      click the button to display profile info
      via JSON.
    </p>
    ...

The first thing you’ll want to do is to be able to read the input for the user name. In jQuery, you accomplish this by:

var username = $('#ghusername').val();

But in Vue, the approach is different. You want to expose a piece of data from the Vue object, and that be changed as the user interacts with it. For example, let’s add the data section to the Vue object:

var app = new Vue({
  el: "#w",
  data: {
    userName: ""
  },
});

The data property is a list of the data that you want to share with the markup. In this case, you’ll start with just a single property to hold the name of the user. In the markup, you’ll use an attribute called v-model to have the input field tied to the userName:

<input type="text" class="form-control"
  placeholder="Github username..."
  autofocus="autofocus"
  v-model="userName">

The v-model is used to create a two-way binding to the userName. This means that if code changes the value, it will show up in the UI; and if the user changes the value, the property value is changed too. Let’s see how you’d work with this new value.

Handling the Click Event

The trick to handling the click event is to use another attribute: v-on. This attribute allows you to register for events on any DOM object. In this case, you want to handle the click event so you’ll use v-on:click as the full name (click is the DOM event name).

<button class="btn btn-success"
  v-on:click="onSubmit()">
  Pull User Data
</button>

Inside the value for v-on, you can simply define what code to execute. In this example, you can just call a method on the Vue object. So now you can add the onSubmit method on the Vue object like so:

var app = new Vue({
  el: "#w",
  data: {
    userName: ""
  },
  methods: {
    onSubmit: function () {
    }
  }
});

The magic starts to happen when you can just start writing code in the onSubmit without having to interrogate the DOM. You can do that by just calling the this property inside the function. Vue takes the methods and data elements and attaches them to the this property. For example, you can just access the userName inside the onSubmit method:

    onSubmit: function () {
      if (this.userName) {
        console.log(`User: ${this.userName}`);
      }
    }

The key differentiator for Vue and jQuery is that instead of trying to modify the DOM, you’ll just keep your business logic in JavaScript and let the bindings in the HTML show the changes that happen to the data. Although you can replace jQuery with Vue, you’ll need to change your mindset about how to solve Web development problems. Let’s implement the calls to GitHub to see how you can close the loop using this new mindset.

The key differentiator for Vue and jQuery is that instead of trying to modify the DOM, you’ll just keep your business logic in JavaScript and let the bindings in the HTML show the changes that happen to the data.

Executing the API

This simple page takes the name of the GitHub user and executes a network request. The jQuery version uses jQuery to execute the network requests. Vue purposely doesn’t attempt to do the networking for you. You’re free to use any networking library you want. It could be jQuery (because you likely already know it), but there are also alternatives like axios and even a Vue-specific one called vue-resource. But because you’re converting code, let’s just keep the jQuery networking code.

The existing code uses jQuery’s getJSON to get the responses from the API. Instead of constructing HTML like the old version does (see Figure 1), you’re going to just create a new data property called user that will hold the object from the server:

  data: {
    userName: "",
    user: null
  },

Now that you have the data, you can simplify it by just setting the value to the user property:

        $.getJSON(requri)
          .done((json) => {
            ...
            this.user = json; // just bind
                              // to the data
          })
          ...

This lets you take the all of the jQuery code that constructs the HTML and just move it to markup:

<!-- User Info -->
<div>
  <h2>{{ user.name }}</h2>

The double curly braces syntax here (often called mustache syntax) allows you to call a one-way binding from the data element. In this case, you’ll just take the user’s name and show it in the h2 element. If you run the code looking like this, it will complain because when you first run the page (before you execute the API), it won’t be able to find the name property of the user. To get around this, you can use another attribute that Vue supplies called v-if:

<!-- User Info -->
<div v-if="user">
  <h2>{{ user.name }}</h2>

The v-if attribute here tells Vue to not show anything in this div if the user is false (which in JavaScript means null, empty, or other case for false). This defers showing the entire div until you set the user. The v-if attribute gives you the power to control which parts of the UI to show until it’s needed. You could show the user section, but it would be empty until you have a user.

You want to show more than just the user name, and you’d like to show a link over to the GitHub page too (as seen in Figure 1). To do this, you can add that markup after the user.name:

<!-- User Info -->
<div v-if="user">
  <h2>{{ user.name }}
    <span class="smallname">
      (@<a v-bind:href="user.html_url"
           target="_blank">{{ user.login }}</a>)
          </span>
  </h2>

This introduces another attribute called v-bind. This attribute is used to bind to attributes. In this case, you’re using it to bind to the href of the anchor tag. Note that because you’re using the v-bind attribute, you don’t need the curly braces. Vue interprets what’s inside the quotes as an expression against the Vue object.

In this way, you’re using Vue to specify what to show inside of the HTML, but it’s still valid HTML. This way, your HTML tools will just work.

Collection Binding

In jQuery, you’re used to creating collections of markup in order to create lists and grids. For example, from the jQuery version:

$.each(repositories, function(index) {
  outhtml += `<div class='d-inline'>
            <a href=" ${repositories[index].html_url}"
               class="btn btn-sm btn-info m-1"
               target="_blank">
                 ${repositories[index].name}
           </a>
        </div>`;
});

By going through a collection of the repositories, the jQuery code constructs a set of divs that represent each of the repositories to show the user. The strategy with Vue is the opposite of this. In Vue, you simply add the repo to the existing user object (because you need two calls to the GitHub API to get both the repos and the user object) like so:

$.getJSON(repouri)
  .done(repos => {
      Vue.set(this.user, "repos", repos)
  })

Before you make the collection work, let’s talk about what’s happening here. The call to Vue.set is something new. It’s actually uncommonly used, but you need it here. Before it makes sense, I need to talk about how Vue updates the UI when you change the code.

In Vue’s data object, you have properties that you want to expose to the UI:

  data: {
    userName: "",
    user: null
  },

Vue takes this list of properties and replaces them at runtime with a set of getters and setters (e.g., properties). It does this so that when you set a value, it can react to that change (you know, that reactivity term that you’ve probably heard bandied about). It does this under the hood. So that when you change a value, it queues up that change so that next time it updates the markup, it knows that your change needs to be shown.

Great, but what is this Vue.set, then? Sometimes you write code that breaks the idea of reactivity and I’m doing that in this example. By adding a new property to user (e.g., repos), Vue didn’t know about it initially. So, you can let Vue know about it by using the Vue.set method. This takes the reactive object to set a property to, the name of the property on the reactive property, and the value to assign it. This way, you’re hinting to Vue that it needs to update the value when you change it. Not necessarily in most cases, but it’s good to know when you do need it.

With all that in place, you can then use a new Vue attribute called v-for to show the collection in the markup:

<a v-for="repo in user.repos"
  v-bind:key="repo.html_url"
  v-bind:href="repo.html_url"
  class="btn btn-sm btn-info m-1">
  {{ repo.name }}
</a>

The v-for attribute instructs the element to create an anchor tag for every repository in the user.repo property and to create a local variable called repo to represent the individual repository.

When using v-for, you also need to specify a unique key for each record (which helps Vue remove items from the UI when you change the collection in the code). You do that by binding to a key attribute with v-bind. In this case, you know that the URL is unique, so just use it as a key. You could use a primary key or other unique key. It doesn’t matter as long as it’s unique.

The rest of the code is just simple binding like you’ve seen before (setting the href and the contents of the anchor). You can see the complete finished code in Listings 3 and 4.

Where Are We?

As a jQuery user, you may be used to being able to create simple applications by just dropping jQuery and writing some quick code. I would argue that Vue is a better choice these days. It allows you to quickly get your job done and keep the code from becoming a spaghetti mess of callbacks and inline HTML. But it does require a learning curve that can be a little steep at first. The switch of mindset from querying and manipulating the DOM to having the DOM react to changes in your code is a key change. But once you get over that hump, I think you’ll see the benefit of using a framework like Vue.