This document provides an introduction to the key concepts and components of Backbone.js, including Models, Collections, Views, Events, Templates, and Routers. It demonstrates how to define each of these components and tie them together, such as creating a Model and View, listening to events on a Collection, and rendering templates. It also notes that Backbone supports inheritance to reduce boilerplate code when building applications with these patterns.
6. Model
var Todo = Backbone.Model.extend({
urlRoot: “/api/todos”,
toggle: function() {
this.save({done: !this.get("done")});
}
});
var todo = new Todo({“id”: “1”});
todo.fetch();
todo.toggle();
8. Collection
var Todos = Backbone.Collection.extend({
url: “/api/todos”,
model: Todo,
});
var todos = new Todos();
todos.fetch();
var todo = todos.get(“1”);
todo.toggle();