Monday, November 18, 2024
Google search engine
HomeLanguagesJavascriptBackbone.js extend Collection

Backbone.js extend Collection

In this article, we will see the Backbone.js extend collection. The Backbone.js extend collection can be used to extend the backbone’s collection class in which we can create our own collection. It also facilitates the instance properties & the optional classProperties that are attached to the constructor function of the collection directly.

Syntax:

Backbone.Collection.extend(properties, classProperties)     

Parameters: It will take 2 parameters, which are described below:

  • properties: This parameter provides the instance properties for the specified collection class.
  • classProperties: This class property is attached to the collection’s constructor function.

Example 1: In this example, we will extend a collection by creating one value.

HTML




<!DOCTYPE html>
<html>
  
<head>
    <title>
      Backbone.js extend Collection
    </title>
    <script src=
            type="text/javascript">
    </script>
    <script src=
            type="text/javascript">
    </script>
    <script src=
            type="text/javascript">
    </script>
</head>
  
<body>
    <script>
        var data = Backbone.Model.extend({
            defaults: {
                id: '1',
                name: 'neveropen User',
            },
        });
        var data1 = Backbone.Collection.extend({
            model: data,
        });
        var final = new data1({});
        document.write('Values : ', JSON.stringify(final));
    </script>
</body>
</html>


Output:

Values : [{"id":"1","name":"neveropen User"}]

Example 2:  In this example, we will extend a collection by creating multiple values.

HTML




<!DOCTYPE html>
<html>
  
<head>
    <title>
       Backbone.js extend Collection
    </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 = Backbone.Model.extend({
            defaults: {
                id: '1',
                name: 'neveropen User',
                address: 'New Delhi',
            },
        });
        var data1 = Backbone.Collection.extend({
            model: data,
        });
        var final = new data1({});
        document.write('Values : ', JSON.stringify(final));
    </script>
</body>
</html>


Output:

Values : [{"id":"1","name":"neveropen User","address":"New Delhi"}]

Reference: https://backbonejs.org/#Collection-extend

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