CodesJava

Easy learning with example program codes

Backbone.JS FindWhere() collection


The Backbone.JS FindWhere() collection method is used to get the first model that matches the specified attribute in the collection.

Syntax:

Collection.FindWhere ( attribute )

Parameters:
attribute: This parameter is used to specify the attribute of a model in a collection.

Example:

<!DOCTYPE html>  
<html>
<head>  
<title>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">  
var X = Backbone.Model.extend({  
defaults: {   
name: "", 
id:""
}  
});   
var Coll = Backbone.Collection.extend({  
model: X    
});  
$(function(){  
var names = new Coll();   
names.set([
{name: "Joy", id: 10},  
{name: "Happy", id: 10},  
{name: "Smiley", id: 20}    ]);  
var Y = names.findWhere({id:10});   
document.write("The values of matched attribute are: ",JSON.stringify(Y));  
});  
</script>  
</body>  
</html>

Output:

The values of matched attribute are: {"name":"Happy","id":10}

Explanation:
In the above example, the FindWhere() method returns the first matched model for the “id: 10” in the collection.

Please follow and like us:
Posted in Java   


Copyright © 2019 CodesJava DMCA.com Protection Status SiteMap Reference: Java Wiki