Friday, September 19, 2025
HomeLanguagesJavascriptBackbone.js fetch Collection

Backbone.js fetch Collection

The Backbone.js fetch Collection is used to fetch the set of models from the server for collection. When the models are returned models are merged with the existing models. If we want to reset all the models and form fill new models we use { reset: true } option of this method which resets the collection. 

Syntax: 

collection.fetch( options );

Parameters: 

  • options: This option hash takes success and error callbacks which will be in case of status of response from the server. 

Example 1: In this example, we will illustrate the Backbone.js fetch Collection. Here we will use the fetch() method and which will call the sync method of the collection which will print the models of the collection.

HTML




<!DOCTYPE html>
<html>
    
<head>
    <title>BackboneJS fetch Router</title>
    <script src="https://code.jquery.com/jquery-2.1.3.min.js"
        type="text/javascript">
    </script>
    <script src=
        type="text/javascript">
    </script>
    <script src=
        type="text/javascript">
    </script>
</head>
  
<body>
    <h1 style="color: green;">
        neveropen
    </h1>
  
    <h3>BackboneJS fetch Router</h3>
  
    <script type="text/javascript">
        var Book = Backbone.Model.extend();
  
        var b1 = { 
            title: "Ram", 
            Author: "Amish Tripathi", 
            vol: 1 
        };
          
        var b2 = { 
            title: "Lolita", 
            Author: "Vladimir Nabokov", 
            vol: 2 
        };
          
        var b3 = { 
            title: "The Palace of Illusion", 
            Author: "Chitra Banerjee", 
            vol: 1 
        };
  
        var books = Backbone.Collection.extend({
            model: Book,
        });
  
        var Library = new books([b1, b2, b3]);
  
        Backbone.sync = function (method, model) {
            for (let x of model.models)
                document.write(JSON.stringify(x), '<br>');
        };
          
        Library.fetch();
    </script>
</body>
  
</html>


Output:

Backbone.js fetch Collection

Example 2: In this example, we will fetch the data from the API and parse the data and get the username from each data.

HTML




<!DOCTYPE html>
<html>
  
<head>
    <title>BackboneJS fetch collection</title>
    <script src="https://code.jquery.com/jquery-2.1.3.min.js"
        type="text/javascript">
    </script>
    <script src=
        type="text/javascript">
    </script>
    <script src=
        type="text/javascript">
    </script>
</head>
  
<body>
    <h1 style="color: green;">
        neveropen
    </h1>
  
    <h3>BackboneJS fetch collection</h3>
      
    <div id='para'></div>
      
    <script type="text/javascript">
        function user_Name(user) {
            console.log(user.username);
        }
  
        var post = Backbone.Model.extend();
  
        var posts = Backbone.Collection.extend({
            model: post,
            url: "jsondata/users",
  
            parse: function (response, options) {
                _.each(response, user_Name);
            }
        });
  
        var comments = new posts();
        comments.fetch();
    </script>
</body>
  
</html>


Output:

Backbone.js fetch Collection

Reference: https://backbonejs.org/#Collection-fetch

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32301 POSTS0 COMMENTS
Milvus
84 POSTS0 COMMENTS
Nango Kala
6665 POSTS0 COMMENTS
Nicole Veronica
11840 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11898 POSTS0 COMMENTS
Shaida Kate Naidoo
6781 POSTS0 COMMENTS
Ted Musemwa
7056 POSTS0 COMMENTS
Thapelo Manthata
6739 POSTS0 COMMENTS
Umr Jansen
6744 POSTS0 COMMENTS