---
title: "Introduction to Backbone.js"  
description: "In this article, I’m explaining about Backbone.js."  
author: "Anonymous User"  
published: 2014-10-14  
updated: 2019-09-07  
canonical: https://www.mindstick.com/articles/1514/introduction-to-backbone-js  
category: "single page app"  
tags: ["javascript", "single page app"]  
reading_time: 3 minutes  

---

# Introduction to Backbone.js

In this article, I’m explaining about Backbone.js.\

in my previous post i'll discuss about Hashtable and dicitionary if you want to learn about hashtable read my previous post Hashtable And Dictionary in C#

A Single-Page Application (SPA), also known as Single-Page Interface (SPI), is a [web application](https://www.mindstick.com/articles/13069/progressive-web-application-pwas-all-you-need-to-know-about) or web site that fits on a single web page with the goal of providing a more fluid [user experience](https://www.mindstick.com/articles/12731/the-importance-of-feedback-to-the-user-experience) similar to a [desktop application](https://www.mindstick.com/forum/23041/i-want-to-devlop-a-desktop-application-in-dot-net-like-aura-reader-for-astrologer).

Backbone.js is a lightweight JavaScript library that adds structure to your client-side code. Developers commonly use libraries like Backbone.js to create single-page applications (SPAs). SPAs are [web applications](https://www.mindstick.com/blog/302483/full-stack-development-the-complete-guide-to-building-modern-web-applications) that load into the browser and then react to [data changes](https://www.mindstick.com/forum/161559/how-can-you-track-data-changes-over-time-without-using-change-data-capture-cdc) on the [client side](https://www.mindstick.com/forum/160418/what-are-the-best-practices-for-securely-storing-bearer-tokens-on-the-client-side) without requiring complete page refreshes from the server.

## Model:

A Model in backbone consists of the connected data for the web application. It also contains the connection among the data. It contains the interactive data.

## View:

The view is the [user interface](https://www.mindstick.com/blog/302016/what-is-user-interface-and-what-are-its-benefits) but in backbone it is not only the view but also a controller for connecting the model to the data interaction.

## Collection:

Collections are the set of models, we create the collection by extending the Backbone.Collection. [While creating](https://www.mindstick.com/forum/23125/something-happened-while-creating-a-switch-windows-phone-8-emulator-error) the collection we want to pass the property that specifies the specific model that the collection will hold.

## Step 1:

Open [visual Studio](https://www.mindstick.com/articles/12378/visual-studio-for-mac-is-out-of-beta-preview-now-officially-available) >> File >> New >> Website >> Select Asp.NET Empty Website >> Give the project Name >> Click “OK” ![Introduction to Backbone.js](https://www.mindstick.com/mindstickarticle/8fb7cad3-81e6-4397-8a11-995e53b2fb97/images/ea65c14f-a536-49c8-988b-334de434f1c0.png)

## Step 2:

Now add html page

## Step 3:

Now Download these three files and add these files in your html page

<script src="script/jquery-2.1.0.min.js"></script>

<script src="script/underscore-min.js"></script>

<script src="script/backbone-min.js"></script>

## Step 4:

Now add this code in your html file

```
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head>    <title></title>    <script src="script/jquery-2.1.0.min.js"></script>    <script src="script/underscore-min.js"></script>    <script src="script/backbone-min.js"></script></head><body><div id="divClient">        EmailID: <input type="text" id="txtIdClient" placeholder="Username"/><br /><br />          Password: <input type="password" id="txtNomClient" placeholder="Paasword"/><br /><br/>          <button id="cmdAddClient">SignUp</button>          <button id="login">Login</button>        <br>        <ul id="listeClient"></ul>    </div></body><script>    var emailid, pass;    var Client = Backbone.Model.extend({        defaults: {            name: null,            pwd: null        },        initialize: function () {            console.log("initialize client");        }    });    var ClientsCollection = Backbone.Collection.extend({        model: Client,        initialize: function () {            console.log("initialize clients collection");            this.bind("add", function (model) { console.log("Add", model.get('id'), model); });            this.bind("remove", function (el) { console.log("Remove", el.get('id'), el); });        }    });    var ClientView = Backbone.View.extend({        el: $("#divClient"),        initialize: function () {            var that = this;            this.listeClients = new ClientsCollection();            this.listClients = new ClientsCollection();            this.listeClients.bind("add", function (model) {                that.addClientToList(model);            });            this.listClients.bind("add", function (model) {                that.addLoginToList(model);            });        },        events: {            'click #cmdAddClient': 'cmdAddClient_Click',            'click #login': 'login'        },        cmdAddClient_Click: function () {            var tmpClient = new Client({                name: $("#txtIdClient").val(),                pwd: $("#txtNomClient").val(),            });            this.listeClients.add(tmpClient);        },        login: function () {            var tmplogin = new Client({                name: $("#txtIdClient").val(),                pwd: $("#txtNomClient").val(),            });            this.listClients.add(tmplogin);        },        addClientToList: function (model) {            emailid = model.get('name');            pass = model.get('pwd');            $("#listeClient").html("<font color=#00ff00>You are Successfully Registered, Now you can Login</font>");        },        addLoginToList: function (model) {;            if (model.get('name') == emailid && model.get('pwd') == pass) {                $("#divClient").html("<font color=blue>Login sucessfull</font>");            }            else {                $("#listeClient").html("<font color=#ff0000>Failed Logged in, Retry</font>");            }        }    });    var clientView = new ClientView();    Backbone.history.start();</script></html>
```

Now execute the your html file it display the like this

![Introduction to Backbone.js](https://www.mindstick.com/mindstickarticle/8fb7cad3-81e6-4397-8a11-995e53b2fb97/images/c13af757-e344-4485-be5c-39fe556480c0.png)\

Now enter emailid and password and click login before signup then will through the [error message](https://www.mindstick.com/forum/23174/error-message-the-page-you-are-requesting-cannot-be-served-because-of-the-extension-configuration) like this

![Introduction to Backbone.js](https://www.mindstick.com/mindstickarticle/8fb7cad3-81e6-4397-8a11-995e53b2fb97/images/98f5b2c4-43ce-4184-8de4-747e2dc6db6a.png)\

Now enter emailid and password and click Signup then it display message like this

![Introduction to Backbone.js](https://www.mindstick.com/mindstickarticle/8fb7cad3-81e6-4397-8a11-995e53b2fb97/images/64c4f658-94c8-4ffe-b85b-589d915d329c.png)\

Now we can login.

![Introduction to Backbone.js](https://www.mindstick.com/mindstickarticle/8fb7cad3-81e6-4397-8a11-995e53b2fb97/images/90bc642e-2624-47d4-a274-d6c64133c25c.png)\

in my next post i'll introduced about SignalR

---

Original Source: https://www.mindstick.com/articles/1514/introduction-to-backbone-js

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
