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

Getters and setters


While the attributes property is (behind the scenes) just a JavaScript object, that doesn't mean that we should treat it as such. For instance, you should never set an attribute directly by changing the attributes property of a Model class:

var book = new Book({pages: 200});
book.attributes.pages = 100; // don't do this!

Instead, the attributes of Backbone's Models should be set using the Model's set method:

book.set('pages', 100); // do this!

The set method has two forms or signatures. The first (shown previously) takes two arguments: one for the data's key and one for its value. This form is great if you only want to set one value at a time, but if you need to set multiple values, you can use the second form instead, which takes only a single argument containing all the values of set:

book.set({pages: 50, currentPage: 49});

In addition, Models also have an unset method, which takes only a single key argument and works in a similar way as JavaScript's delete statement, except...