Sunday, November 17, 2024
Google search engine
HomeLanguagesJavascriptBackbone.js sync Model

Backbone.js sync Model

Backbone.js sync Model is the function that the model calls every time it attempts to read or save a model to the server. When a model begins a sync with the server, a request event is emitted. If the request completes successfully you’ll get a sync event and an error event if not. 

Syntax: 

model.sync( method, model, options );

Parameters: It takes the following parameters:

  • method: It is the CRUD method, which stands for C for create, R for read, U for update, and D for delete
  • model: It is a model which has to save, or read.
  • options: It is success and error callbacks and other jQuery request options.

Example#1: In this example, we will describe the Backbone.js sync Model with read and delete method.

HTML




<!DOCTYPE html>
<html>
  
<head>
    <title>BackboneJS Model sync</title>
    <script src=
            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 Model sync</h3>
  
    <script type="text/javascript">
  
  
        var Geek = new Backbone.Model({
            id: "1001e",
            Name: "Cody"
        });
  
        Geek.sync = function (method, model) {
            document.write(`This is sync function which is 
            called for ${method} an model ` + 
                           JSON.stringify(model), '<br>');
        };
  
        Geek.fetch();
        Geek.destroy();
    </script>
</body>
  
</html>


Output:

Backbone.js sync model

Example#2: In this example we will describe the Backbone.js sync model with update method.

HTML




<!DOCTYPE html>
<html>
  
<head>
    <title>BackboneJS Model sync</title>
    <script src=
            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 Model sync</h3>
  
  
  
  
    <script type="text/javascript">
        Backbone.sync = function (method, model) {
            document.write(`This is sync function which is 
            called for ${method} an model ` + 
                           JSON.stringify(model), '<br>');
        };
  
        var Geek = new Backbone.Model({
            id: "1001e",
            Name: "Cody"
        });
  
        Geek.sync = function (method, model) {
            document.write(`This is sync function which is 
            called for ${method} an model ` + 
                           JSON.stringify(model), '<br>');
        };
  
        Geek.save();
        Geek.save({ id: "1002e", Name: "zetshu" });
    </script>
</body>
  
</html>


Output:

Backbone.js sync model

Reference: https://backbonejs.org/#Model-sync

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