- BackboneJS Tutorial
- BackboneJS - Home
- BackboneJS - Overview
- BackboneJS - Environment Setup
- BackboneJS - Applications
- BackboneJS - Events
- BackboneJS - Model
- BackboneJS - Collection
- BackboneJS - Router
- BackboneJS - History
- BackboneJS - Sync
- BackboneJS - View
- BackboneJS - Utility
- BackboneJS Useful Resources
- BackboneJS - Quick Guide
- BackboneJS - Resources
- BackboneJS - Discussion
BackboneJS - Collection Where
Description
It is used to display the model by using the matched attribute in the collection.
Syntax
collection.where(attribute)
Parameters
attribute − It represents the property of a defined model.
Example
<!DOCTYPE html> <html> <head> <title>Collection Example</title> <script src = "https://code.jquery.com/jquery-2.1.3.min.js" type = "text/javascript"></script> <script src = "https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.2/underscore-min.js" type = "text/javascript"></script> <script src = "https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.2/backbone-min.js" type = "text/javascript"></script> </head> <body> <script type = "text/javascript"> //'Player' is a model name Player = Backbone.Model.extend ({ name: "" }); //The 'PlayersCollection' is an instance of the collection PlayersCollection = Backbone.Collection.extend ({ model: Player //The model 'Player' is specified by overriding the "model" property of the collection }); var player1 = new Player({ name: "Dravid" }); var player2 = new Player({ name: "Raina"}); var player3 = new Player({ name: "Jadeja"}); var mycollection = new PlayersCollection(); //The 'player1','player2' and 'player3' are 3 instances added to the collection by using 'mycollection' instance mycollection.add(player1); mycollection.add(player2); mycollection.add(player3); //The where() method returns the model, which contains the name with "Raina" in the collection var myteam = mycollection.where({ name: 'Raina' }); document.write("Total numbers of items that matches given attribute are:", +myteam.length); </script> </body> </html>
Output
Let us carry out the following steps to see how the above code works −
Save the above code in the where.htm file.
Open this HTML file in a browser.
backbonejs_collection.htm
Advertisements