CodesJava

Easy learning with example program codes

Backbone.JS Get() collection


The Backbone.JS Get() collection method retrieves a model from a collection by using id or Cid.

Syntax:

Backbone.Collection.Get (id )   

Parameters:
id: This parameter is used to specify the id of the model to be retrieved from the 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 = new Backbone.Collection();  
X.on("change:msg", function(model) {   
document.write("Hello " + model.get('msg'));  
});  
X.add({id: 100});  
var Y = X.get(100);  
Y.set('msg', 'World!');  
</script>  
</body>  
</html>

Output:

Hello World!

Explanation:
In the above example, the Y object gets the X with id = 100.

Please follow and like us:


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