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:
<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>
<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>Hello World!
<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>
<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>Hello World!
Example Hello World!
Output:
Hello World!
Hello World!
Hello World!
Explanation:
In the above example, the Y object gets the X with id = 100.