Open In App

Backbone.js get Model

Improve
Improve
Like Article
Like
Save
Share
Report

The Backbone.js get model is used to return a value of a particular attribute from the model.

Syntax:

Backbone.Model.get(attribute)

Parameter: It accepts an only a single parameter:

  • attribute: It is used to define the attribute for the created model.

Example 1: In this example, we will return the attributes of the book model.

HTML




<!DOCTYPE html>
<html>
  
<head>
    <title>Backbone.js get Model</title>
    <script src=
            type="text/javascript">
    </script>
    <script src=
            type="text/javascript">
    </script>
    <script src=
            type="text/javascript">
    </script>
</head>
<body>    
    <script type="text/javascript">
        var Books = Backbone.Model.extend();
        var book = new Books({
            bookid: 23,
            price: 678,
            book_name: "css"
        });
        document.write(' Values: ' + JSON.stringify(book));
        document.write("<br>");
        document.write('bookid: ', book.get('bookid'));
        document.write("<br>");
        document.write('price: ', book.get('price'));
        document.write("<br>");
        document.write('book_name: ', book.get('book_name'));
    </script>
</body>
</html>


Output:

Values: {"bookid":23,"price":678,"book_name":"css"}
bookid: 23
price: 678
book_name: css

Example 2: In this example, we will modify the value and get the values.

HTML




<!DOCTYPE html>
<html>
  
<head>
    <title>Backbone.js get Model</title>
    <script src=
            type="text/javascript">
    </script>
    <script src=
            type="text/javascript">
    </script>
    <script src=
            type="text/javascript">
    </script>
</head>
  
<body>
    <script type="text/javascript">
        var Books = Backbone.Model.extend();
        var book = new Books({
            bookid: 23,
            price: 678,
            book_name: "css"
        });
        document.write(' Values: ' + JSON.stringify(book));
        book.set({
            bookid: 33,
            price: 211,
            book_name: "php"
        });
        document.write("<br>");
        document.write('book_name: ', book.get('book_name'));
    </script>
</body>
</html>


Output:

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

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



Last Updated : 17 Jun, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads