The Backbone.JS Clone() collection method is used to get the shallow copy of the specified object.
Syntax:
Collection.Clone ( )
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 = Backbone.Model.extend();
var Y =new X({
name: 'Tom Jones'
});
var Coll = Backbone.Collection.extend({
model: X
});
var Z = new Coll();
var value = Z.clone(Y.get('name'));
value.name="Jerry Jones";
document.write("New Collection's Instance: ", JSON.stringify(value.name));
</script>New Collection's Instance: "Jerry Jones"
<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 = Backbone.Model.extend();
var Y =new X({
name: 'Tom Jones'
});
var Coll = Backbone.Collection.extend({
model: X
});
var Z = new Coll();
var value = Z.clone(Y.get('name'));
value.name="Jerry Jones";
document.write("New Collection's Instance: ", JSON.stringify(value.name));
</script>New Collection's Instance: "Jerry Jones"
Example New Collection's Instance: "Jerry Jones"
Output:
New Collection's Instance: "Jerry Jones"
New Collection's Instance: "Jerry Jones"
New Collection's Instance: "Jerry Jones"
Explanation:
In the above example, the Clone() method used the Get() method to retrieve the ‘name’ attribute of the model.