Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8850305
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:52:16+00:00 2026-06-14T12:52:16+00:00

I’m new to backbone, so please bear with me. I would like to create

  • 0

I’m new to backbone, so please bear with me. I would like to create a collection in which the models all have a handful of critical attributes which they share as well as a number of other attributes which they do not share. I thought the best way to do this would be to extend a superclass model (containing defaults for all of the shared attributes) such that when I instantiate a new subclass model, those attributes are initialized and additional attributes specific to the subclass are also added to the model. I don’t know how to accomplish this, but here is the direction I’ve been working in:

app.Fruit = Backbone.Model.extend(
{

    defaults: {
        name: "none",
        classification: "none",
        color: "none"
    },

    initialize: function()
    {
        console.log("Fruit Initialized");
    }

});

app.Apple = app.Fruit.extend(
{

    url: "/php/Apple.php",

    initialize: function()
    {
        console.log("Apple initialized");
        // somehow fetch additional information from server
        // and add sublcass-specific attributes to model
        // (for example, in the case of an apple, an attribute called cultivar)
    }

});


var FruitCollection = Backbone.Collection.extend(
{

    model: function(attributes, options)
    {

        switch(attributes.name)
        {

        case "Apple":
            return new app.Apple(attributes, options);
            break;

            default:
            return new app.Fruit(attributes, options);
            break;

        }

    }

    // ...

});

app.fruitCollectionCurrent = new FruitCollection([
    {name: "Apple"},
    {name: "Orange"}
]);

// logs: Fruit Initialized

Any suggestions on how to properly initialize a subclass with additional attributes would be appreciated.

Thanks.

EDIT: THE SOLUTION

I thought I would post the code that ended up working for me… Arrived at it thanks to @Mohsen:

app.Fruit = Backbone.Model.extend(
{

    defaults: {
        name: "none",
        classification: "none",
        color: "none"
    },

    initialize: function()
    {
        console.log("Fruit Initialized");
    }

});

app.Apple = app.Fruit.extend(
{

    url: "/php/Apple.php",

    initialize: function()
    {
        console.log("Apple initialized");
        return this.fetch();
    }

});

I didn’t even really need the asynchronous call in the subclass because I wasn’t fetching any additional data for Fruit (Fruit’s attributes were just set in the constructor), only for Apple. What I was really looking for was the call to this.fetch() with the specified URL. Sorry if the question made things seem more complex…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T12:52:18+00:00Added an answer on June 14, 2026 at 12:52 pm

    Backbone is not easy to work with when it comes to hierarchy. I solve this problem by calling parent model/collection initializer inside of my child model/collection initializer.

    Fruit = Backbone.Model.extend({
        url: "/fruits:id",
        initialize: function initialize () {
            this.set('isWashed', false);
            return this.fetch();
        }
    });
    
    Apple = Fruit.extend({
        url: "/fruits/apples:id"
        initialize: function initialize () {
                var that = this;
            Fruit.prototype.initialize.call(this, arguments).then(function(){
                that.fetch();
            })
            this.set("hasSeed", true);
        }
    });
    

    Now your Apple model does have all properties of a Fruit.

    Key line is Fruit.prototype.initialize.call(this, arguments);. You call initialize method of Fruit for Apple.

    You can also use this.__super__ to access parent model:

    this.__super__.prototype.initialize.call(this, arguments);

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I would like to run a str_replace or preg_replace which looks for certain words
I have a text area in my form which accepts all possible characters from
I would like to count the length of a string with PHP. The string
I have an autohotkey script which looks up a word in a bilingual dictionary
I have an array which has BIG numbers and small numbers in it. I
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display
Let's say I'm outputting a post title and in our database, it's Hello Y’all
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I'm new to using the Perl treebuilder module for HTML parsing and can't figure

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.