9951 explained code solutions for 126 technologies


backbone.jsHow can I use Backbone.js for both frontend and backend development?


Backbone.js is a JavaScript library that can be used for both frontend and backend development. It provides structure to web applications by providing models with key-value binding and custom events, collections with a rich API of enumerable functions, views with declarative event handling, and connects it all to your existing API over a RESTful JSON interface.

For frontend development, Backbone.js provides the structure necessary to create single-page applications. For example, the following code creates a basic view that renders a template and adds it to the page when instantiated:

var MyView = Backbone.View.extend({
  render: function(){
    this.$el.html(this.template());
    return this;
  }
});

var myView = new MyView();
$("body").append(myView.render().el);

For backend development, Backbone.js can be used to create a RESTful API. This API can then be used to communicate with the frontend. For example, the following code creates a basic model that can be used to communicate with a server-side API:

var MyModel = Backbone.Model.extend({
  urlRoot: '/api/mymodel',
  initialize: function(){
    this.on('change', function(){
      this.save();
    });
  }
});

var myModel = new MyModel();
myModel.fetch(); // fetches data from the server-side API

In summary, Backbone.js can be used for both frontend and backend development. It provides the structure necessary to create single-page applications on the frontend and a RESTful API on the backend.

Helpful links

Edit this code on GitHub