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

  • Home
  • SEARCH
  • 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 7780857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T19:02:24+00:00 2026-06-01T19:02:24+00:00

Taking the following Model: MyModel= Backbone.Model.extend({ defaults : { myNestedModel:undefined, }, initialize: function() {

  • 0

Taking the following Model:

MyModel= Backbone.Model.extend({
  defaults : {
    myNestedModel:undefined,
  },
  initialize: function() {
    this.set({myNestedModel: new MyNestedModel());
  }
});

It has a single property named ‘myNestedModel’ which has the following definition:

MyNestedModel= Backbone.Model.extend({
  defaults : {
    myModel:undefined,
  }
});

It too has a single Property name ‘myModel’. Now if I create an instance of MyModel:

aModel = new MyModel();

The nested model will have been set in MyModel’s initialize method. I then use JSON.stringify in a two step process:

// Use Backbone.js framework to get an object that we can use JSON.stringfy on
var modelAsJson = aModel.toJSON();

// Now actually do stringify 
var modelAsJsonString = JSON.stringify(modelAsJson);

This works fine and I get the JSON representation of MyModel and it’s property of MyNestedModel. The problem occurs when I use defaults, for example:

MyModel= Backbone.Model.extend({
      defaults : {
        new MyNestedModel(),
      }
    });

This causes a problem with JSON.stringify since it doesn’t support circular references. I assume the circular reference is being created because all instances of MyModel share the same instance of MyNestedModel. Whereas the initialize method creates a new nested model for each instance.

Questions:

  1. Is my understanding of defaults:{} being the ’cause’ of the
    problem correct?
  2. From a question I posted recently I got the
    impression I should be using defaults for all properties. If that is
    the case, how should I be using defaults in the scenario presented
    in this post/question?
  3. Can someone clarify the use of defaults:{}
    with regards to when the value applies, when it’s overridden and
    whether instances share the same default ‘instances’?
  • 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-01T19:02:25+00:00Added an answer on June 1, 2026 at 7:02 pm

    Defaults is used only for attributes inside your model ( the data in the model ), and whenever you create your model it takes the values from defaults and sets the attributes. e.g.

    User = Backbone.Model.extend({
         defaults : {
             rating : 0
         }
    })
    
    User1 = new User({ name : 'jack', email : 'jack@gmail.com' });
    User2 = new User({ name : 'john', email : 'john@gmail.com' });
    
    User1.set({ rating : 2 });
    

    Now your two models when called with toJSON will print

    {
       rating: 2,
       name: 'jack',
       email: 'jack@gmail.com'
    }
    {
       rating: 0,
       name: 'john',
       email: 'john@gmail.com'
    }
    

    Since defaults is an object, every value you place there is evaluated immediately so :

    defaults : {
        rating : defaultRating()
    }
    

    will call defaultRating() – not everytime when you initialize the model, but immediately ( in the extend method )

    You should use defaults for models where you need some data to exist on the creating of the model ( e.g. new myModel() )

    In your example you have the following mistakes :

    1.set a value without a property

     defaults : {
          PROPERTY : new Model() 
     }
    

    2.you don’t need such an option for your defaults – you should place there only attributes ( data ) for the model

    Defaults applies always as long as it is not replaced by a new defaults in an extended model e.g.

     var Model = Backbone.Model.extend({ defaults : { alpha : 'beta' } });
     var myModel = Model.extend({ defaults : { beta : 'gama' } });
    

    now your myModel when initialized will have

     { beta : 'gama' } // alpha : 'beta' will not be set as value, because it is replaced
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Taking the jQuery framework for example, if you run code like this: $(document).ready(function init()
Taking the following object model: public abstract class Entity { public Guid Id {
I have a simple photo taking application that has the following design: [Consider the
Taking advice from this post , I purchased a copy of 'The C Programming
Taking this article on classes and structs as an example: http://msdn.microsoft.com/en-us/library/ms173109.aspx namespace ProgrammingGuide {
I have a model that has an attribute that is an Array. What's the
When constructing a linear model in R, what is the difference between the following
I was using a textbox to do the following: <input style=width:50px; type=text id=blah value=@model.value1
Suppose that I have the following HBM mapping: <class name=Student table=student> <set name=classes table=student_classes
I've decoupled events in this WPF application in the following way. What is the

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.