The Backbone.JS Fetch() collection method extracts the data from the model in a collection.
Syntax:
Collection.Fetch ( options )
Parameters:
options: This parameter is used to specify the “SUCCESS” or “ERROR” callbacks.
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">
Backbone.sync = function(method, model) {
document.write("Fetched data: ",method + ": " + JSON.stringify(model));
};
var values = new Backbone.Collection({
id:100,
name:"Happy"
});
values.fetch();
</script>Fetched data: read: [{"id":100,"name":"Happy"}]
<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">
Backbone.sync = function(method, model) {
document.write("Fetched data: ",method + ": " + JSON.stringify(model));
};
var values = new Backbone.Collection({
id:100,
name:"Happy"
});
values.fetch();
</script>Fetched data: read: [{"id":100,"name":"Happy"}]
Example Fetched data: read: [{"id":100,"name":"Happy"}]
Output:
Fetched data: read: [{"id":100,"name":"Happy"}]
Fetched data: read: [{"id":100,"name":"Happy"}]
Fetched data: read: [{"id":100,"name":"Happy"}]
Explanation:
In the above example, the Fetch() method retrieves the data of the collection instance ‘values’.