9951 explained code solutions for 126 technologies


backbone.jsHow can I use Backbone.js to create a Model-View-Controller architecture?


Backbone.js is a JavaScript library that can be used to create a Model-View-Controller (MVC) architecture. The library provides models, views, and controllers that can be used to create a well-structured application.

Models are used to store and retrieve data from a database. They also provide methods to manipulate the data, such as validation and syncing with the server.

Views are used to display the data in the models. They can also be used to respond to user input and trigger events.

Controllers are used to connect the models and views together. They can be used to handle user input and update the models and views accordingly.

Here is an example of a simple Backbone.js MVC application:

// Model
var Person = Backbone.Model.extend({
    defaults: {
        name: 'John Doe',
        age: 30
    }
});

// View
var PersonView = Backbone.View.extend({
    render: function() {
        var name = this.model.get('name');
        var age = this.model.get('age');
        this.$el.html('Name: ' + name + '<br>Age: ' + age);
    }
});

// Controller
var PersonController = Backbone.Controller.extend({
    initialize: function() {
        this.model = new Person();
        this.view = new PersonView({model: this.model});
    }
});

// Instantiate the controller
var personController = new PersonController();

// Render the view
personController.view.render();

// Output
// Name: John Doe
// Age: 30

The code above creates a model, view, and controller for a Person object. The model has two attributes, name and age, which are set to default values. The view renders the model data to the page. The controller initializes the model and view.

Helpful links

Edit this code on GitHub