Tuesday, September 24, 2024
Google search engine
HomeLanguagesJavascriptBackbone.js fetch Model

Backbone.js fetch Model

In this article, we will see Backbone.js fetch() model. The Backbone.js fetch() model is used to merge the state of the model with fetched attributes by accepting the model data from the server by delegating the sync() method in the given model.

Syntax:

Backbone.Model.fetch(options);

Parameters: It accepts single parameter value:

  • options: It specifies parameters such as id, name etc. that are used in a model.

Example 1: This example shows the fetch() model for the “book” model data.

HTML




<!DOCTYPE html>
<html>
  
<head>
    <script src=
            type="text/javascript">
    </script>
    <script src=
            type="text/javascript">
    </script>
    <script src=
            type="text/javascript">
    </script>    
</head>
  
<body>
    <script type="text/javascript"
      
        Backbone.sync = function(method1, mymodel) {  
            document.write(JSON.stringify(mymodel));  
        }; 
        var Books = Backbone.Model.extend();  
        var book = new Books({bookid:23}); 
             
        book.fetch();   
    </script
</body>
</html>


Output:

{"bookid":23}

Example 2: This example shows the fetch() model for the “book” model attributes.

HTML




<!DOCTYPE html>
<html>
  
<head>
    <script src=
            type="text/javascript">
    </script>
    <script src=
            type="text/javascript">
    </script>
    <script src=
            type="text/javascript">
    </script>    
</head>
  
<body>
    <script type="text/javascript"
      
        Backbone.sync = function(method1, mymodel) {  
            document.write(JSON.stringify(mymodel));  
        }; 
        var Books = Backbone.Model.extend();  
        var book = new Books({bookid:23,price:678,book_name:'php'}); 
             
        book.fetch();   
    </script
</body>
</html>


Output:

{"bookid":23,"price":678,"book_name":"php"}

Reference: https://backbonejs.org/#Model-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

Recent Comments