Open In App

What are Backbone.js Models ?

Backbone.js model is the heart or main component of a javascript application. It contains the interaction data and logic of the user interaction with the view layer. In other words, it is the brain or logic behind the event triggering and decides how to handle user events and interaction it does conversions, validations, computation, and access control, Model provides us with methods to handle changes.

A model can also be understood as a representation of basic logic or business logic of an application that handles data storage and manipulation.



Syntax:

Backbone.Model.Extend( properties, classProperties )    

Parameter description:



Below are methods associated with Backbone.js model class:

Below are Underscore methods associated with Backbone.js model class:

Example 1:




<!DOCTYPE html>
<html>
  
<head>
    <title>backbone.js model example</title>
    <script src=
            type="text/javascript"></script>
    <script src=
            type="text/javascript"></script>
    <script src=
            type="text/javascript"></script>
</head>
  
<body style="background-color:black;">
    <h1 style="color:green;">GeeksforGeeks</h1>
    <button onclick="invoke()">Show data:</button>
  
    <script type="text/javascript">
        
        // Here we are creating a custom model
          // class by extending it.
        var data_collection = Backbone.Model.extend({
            defaults: {
                website: "GeeksForGeeks",
                rating: 10
            },
        });
        var example = Backbone.Collection.extend({
            model: data_collection
        });
        function invoke() {
            var Z = new example({});
            document.write("Collection: ", 
                           JSON.stringify(Z));
        }
    </script>
</body>
  
</html>

Output:

 

Example 2:




<!DOCTYPE html>
<html>
  
<head>
    <title>backbone model example</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 data_collection = Backbone.Model.extend({
            defaults: {
                website: "GeeksForGeeks",
                rating: 10
            },
        });
        var example = Backbone.Collection.extend({
            model: data_collection
        });
        var Z = new example({});
        window.alert("backbone models example");
        document.write("Backbone model");
        document.write("output: ", JSON.stringify(Z));  
    </script>
</body>
  
</html>

Output:

 

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


Article Tags :