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

Extraction methods


Another way in which several of the Underscore methods can be used is by extracting a specific Model or Models from a Collection. The simplest way to do this is with the where and findWhere methods, which return all the (or in the case of findWhere, the first) Models that match a provided attributes object. For example, if you want to extract all the books in a Collection, which have exactly one hundred pages, you can use the where method, as shown here:

var books = new Backbone.Collection([
    { 
        pages: 100,        title: "Backbone Essentials 5: The Essentials Return"
    }, {
        pages: 100,        title: "Backbone Essentials 6: We're Not Done Yet?"
    },{
        pages: 25,        title: "Completely Fake Title"
    } 
]);
var hundredPageBooks = books.where({pages: 100});
//  hundredPageBooks array of all of the books except Completely Fake Title
var firstHundredPageBook = books.findWhere({pages: 100});
firstHundredPageBook; // Backbone Essentials 5: The...