9951 explained code solutions for 126 technologies


backbone.jsHow can I use the Backbone.js framework to build a web application?


Backbone.js is a JavaScript library that provides an MVC framework for developing web applications. It helps to structure your application's code and provides the necessary tools for creating single-page applications.

To use Backbone.js to build a web application, you need to include the Backbone.js library in your HTML page, create a model, view, and collection, and then bind the model and view together.

For example, the following code creates a simple model:

var Person = Backbone.Model.extend({
  defaults: {
    name: '',
    age: 0
  }
});

The following code creates a view:

var PersonView = Backbone.View.extend({
  tagName: 'li',
  render: function(){
    this.$el.html(this.model.get('name'));
    return this;
  }
});

And the following code binds the model and view together:

var person = new Person({name: 'John', age: 25});
var personView = new PersonView({model: person});
$('#container').html(personView.render().el);
// Output: <li>John</li>

Code explanation

  • Person: a Backbone.js model that stores data about a person
  • PersonView: a Backbone.js view that renders the person's name
  • person: an instance of the Person model
  • personView: an instance of the PersonView view
  • render(): a function that renders the view

For more information about using Backbone.js to build web applications, see the following links:

Edit this code on GitHub