Open In App

Backbone.js clone Model

Last Updated : 13 Jun, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

In this article, we will discuss the Backbone.js clone model. The Backbone.js clone is used to provide a copy from the given model. we can also copy the model to another using clone() method.

Syntax:

Backbone.Model.clone()

Note: It takes no parameters.

Example 1: In this example, we will copy the book model to the chapter model.

HTML
<!DOCTYPE html>
<html>

<head>
    <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 Books = Backbone.Model.extend();
        var book = new Books({ book_name: "HTML", price: 100 });
        document.write("Values in book model :  ", 
                       JSON.stringify(book));
        document.write("<br>");

        // Copy details to chapters using clone() method
        var chapters = book.clone();

        document.write();

        document.write(
          "Values in chapters model (copied from book model) : ", 
          JSON.stringify(chapters));
    </script>
</head>

<body></body>

</html>

Output:

Values in book model : {"book_name":"HTML","price":100}
Values in chapters model (copied from book model) :
    {"book_name":"HTML","price":100}

Example 2: In this example, we will copy the book model to again the book model.

HTML
<!DOCTYPE html>
<html>

<head>
    <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 Books = Backbone.Model.extend();
        var book = new Books({ book_name: "css" });
        document.write("Values in book model :  ", 
                       JSON.stringify(book));
        document.write("<br>");

        // Copy details to book again using clone() method
        var book = book.clone();

        document.write();

        document.write(
          "Values in book model (copied from book model) :  ", 
          JSON.stringify(book));

    </script>
</head>

<body></body>

</html>

Output:

Values in book model : {"book_name":"css"}
Values in book model (copied from book model) 
    : {"book_name":"css"}

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


Next Article

Similar Reads