9951 explained code solutions for 126 technologies


backbone.jsHow do I use Backbone.js to create a simple application for beginners?


Backbone.js is a great framework for creating simple applications. To get started, you'll need to include the Backbone.js library in your HTML. Then, you can create a simple application as follows:

  1. Create a Model to represent the data of your application. The following example creates a Person model with two attributes: name and age.
var Person = Backbone.Model.extend({
  defaults: {
    name: 'John Doe',
    age: 30
  }
});
  1. Create a View to render the data of your application. The following example creates a PersonView that renders the Person model.
var PersonView = Backbone.View.extend({
  render: function() {
    this.$el.html(this.model.get('name') + ' (' + this.model.get('age') + ' years old)');
    return this;
  }
});
  1. Instantiate the Model and View. The following example creates a new Person instance and renders it with the PersonView.
var person = new Person();
var personView = new PersonView({ model: person });
personView.render();

// Output: John Doe (30 years old)
  1. Add event listeners to your View. The following example adds an event listener to the PersonView that increases the age of the Person model when the view is clicked.
var PersonView = Backbone.View.extend({
  events: {
    'click': 'incrementAge'
  },
  render: function() {
    this.$el.html(this.model.get('name') + ' (' + this.model.get('age') + ' years old)');
    return this;
  },
  incrementAge: function() {
    this.model.set('age', this.model.get('age') + 1);
  }
});

By following these steps, you can create a simple application with Backbone.js.

Helpful links

Edit this code on GitHub