Book Image

Backbone.js Essentials

By : Jeremy Walker
Book Image

Backbone.js Essentials

By: Jeremy Walker

Overview of this book

<p>This book offers insight into creating and maintaining dynamic Backbone.js web applications. It delves into the the fundamentals of Backbone.js and helps you achieve mastery of the Backbone library.</p> <p>Starting with Models and Collections, you'll learn how to simplify client-side data management and easily transmit data to and from your server. Next, you'll learn to use Views and Routers to facilitate DOM manipulation and URL control so that your visitors can navigate your entire site without ever leaving the first HTML page. Finally, you'll learn how to combine those building blocks with other tools to achieve high-performance, testable, and maintainable web applications.</p>
Table of Contents (20 chapters)
Backbone.js Essentials
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Index

Server-side actions


Just as with Models, Collections have a fetch method that retrieves data from the server and a save method to send data to the server. One minor difference, however, is that by default, a Collection's fetch will merge any new data from the server with any data it already has. If you prefer to replace your local data with server data entirely, you can pass a {reset: true} option when you fetch. Collections also have the url, parse, and toJSON methods that control how fetch/save work.

All these methods work in the same way as they do on Models. However, Collections do not have urlRoot; while the Models inside a Collection may have IDs, the Collections themselves don't, so Collections don't need to generate their URLs using a .urlRoot:

var Cats = Backbone.Collection.extend({
     url: '/cats'
});
var cats = new Cats({name: 'Garfield'});
cats.save(); // saves Garfield to the server
cats.fetch(); // retrieves cats from the server and adds them