9951 explained code solutions for 126 technologies


backbone.jsHow do I use Backbone.js to create a backend?


Backbone.js is a JavaScript library that can be used to create a backend. It is a Model-View-Presenter (MVP) library that provides structure to your application.

To use Backbone.js to create a backend, you need to create a model that will contain the data for your application. For example, you can create a model for a blog post:

var PostModel = Backbone.Model.extend({
    defaults: {
        title: '',
        content: ''
    }
});

You can then create a collection to store multiple models:

var PostCollection = Backbone.Collection.extend({
    model: PostModel
});

You can then create a view to render the models in the collection:

var PostView = Backbone.View.extend({
    render: function(){
        var html = '';
        this.collection.each(function(post){
            html += '<h1>' + post.get('title') + '</h1>';
            html += '<p>' + post.get('content') + '</p>';
        });
        this.$el.html(html);
    }
});

You can then create a router to handle requests and call the appropriate view:

var Router = Backbone.Router.extend({
    routes: {
        'posts': 'showPosts'
    },
    showPosts: function(){
        var postView = new PostView({
            collection: postCollection
        });
        postView.render();
    }
});

Finally, you can create an instance of the router and start the application:

var router = new Router();
Backbone.history.start();

By using Backbone.js, you can create a backend that will handle requests and render the appropriate view.

Helpful links

Edit this code on GitHub