9951 explained code solutions for 126 technologies


backbone.jsHow do I create a sample project using Backbone.js?


  1. To create a sample project using Backbone.js, you will need to include the Backbone.js library in your HTML page.

  2. You can do this by adding a <script> tag to your HTML page that points to the Backbone.js library:

<script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.3.3/backbone-min.js"></script>
  1. Next, you will need to create a model and view in your JavaScript code. A model is a representation of the data in your application, and a view is the way that the data is displayed.

  2. To create a model, you can use the Backbone.Model constructor:

var Person = Backbone.Model.extend({
    defaults: {
        name: '',
        age: 0
    }
});
  1. To create a view, you can use the Backbone.View constructor:
var PersonView = Backbone.View.extend({
    render: function() {
        this.$el.html(this.model.get('name') + ' is ' + this.model.get('age') + ' years old.');
        return this;
    }
});
  1. Finally, you can create an instance of the model and view and render it to the page:
var person = new Person({name: 'John', age: 25});
var personView = new PersonView({model: person});
$('body').append(personView.render().el);
  1. This will output the following to the page:
John is 25 years old.

## Helpful links

Edit this code on GitHub