What is backbone?
Backbone.js is a lightweight JavaScript library that provides the structure for web applications by offering 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.
What are backbone's main functionalities?
Models
Models in Backbone.js represent the data and the logic of the application. They provide methods for data manipulation and validation.
const Backbone = require('backbone');
const Book = Backbone.Model.extend({
defaults: {
title: 'Unknown',
author: 'Unknown'
}
});
const myBook = new Book({ title: '1984', author: 'George Orwell' });
console.log(myBook.get('title')); // Outputs: 1984
Collections
Collections in Backbone.js are ordered sets of models. They provide a rich set of methods for handling and manipulating groups of models.
const Backbone = require('backbone');
const Book = Backbone.Model.extend({});
const Library = Backbone.Collection.extend({
model: Book
});
const myLibrary = new Library([
{ title: '1984', author: 'George Orwell' },
{ title: 'Brave New World', author: 'Aldous Huxley' }
]);
console.log(myLibrary.length); // Outputs: 2
Views
Views in Backbone.js are responsible for rendering the user interface and handling user interactions. They are associated with a DOM element and a model.
const Backbone = require('backbone');
const BookView = Backbone.View.extend({
tagName: 'li',
render: function() {
this.$el.html(this.model.get('title'));
return this;
}
});
const book = new Backbone.Model({ title: '1984' });
const bookView = new BookView({ model: book });
console.log(bookView.render().el.outerHTML); // Outputs: <li>1984</li>
Routers
Routers in Backbone.js provide methods for routing client-side pages and connecting them to actions and events.
const Backbone = require('backbone');
const AppRouter = Backbone.Router.extend({
routes: {
'books/:id': 'showBook'
},
showBook: function(id) {
console.log('Book ID:', id);
}
});
const router = new AppRouter();
Backbone.history.start();
router.navigate('books/1', { trigger: true }); // Outputs: Book ID: 1
Other packages similar to backbone
react
React is a JavaScript library for building user interfaces. It allows developers to create large web applications that can update and render efficiently in response to data changes. Unlike Backbone, React focuses on the view layer and uses a component-based architecture.
angular
Angular is a platform and framework for building single-page client applications using HTML and TypeScript. It provides a comprehensive solution for building dynamic web applications, including data binding, dependency injection, and a powerful templating system. Angular is more opinionated and feature-rich compared to Backbone.
vue
Vue.js is a progressive JavaScript framework for building user interfaces. It is designed to be incrementally adoptable and focuses on the view layer. Vue provides a reactive data binding system and a component-based architecture, similar to React but with a more flexible and less opinionated approach compared to Angular.
| _ \ | | | | ()
| |) | __ _ | | | | ___ _ __ ___ _ ___
| _ < / ` |/ | |/ /| ' \ / _ | ' \ / _ \ | / __|
| |) | (| | (| < | |) | () | | | | /| _
|/ _,|_||_|./ _/|| ||_() |/
_/ |
|/
('__________________________________________________')
(.———————————————————————————————————————————————————._)
Backbone supplies structure to JavaScript-heavy applications by providing models 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 application over a RESTful JSON interface.
For Docs, License, Tests, and pre-packed downloads, see:
http://documentcloud.github.com/backbone/
To suggest a feature, report a bug, or general discussion:
http://github.com/documentcloud/backbone/issues/
All contributors are listed here:
http://github.com/documentcloud/backbone/contributors
Special thanks to Robert Kieffer for the original philosophy behind Backbone.
http://github.com/broofa