Backbone.js - Using new () in standard defaults - circular reference

Taking the following model:

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

      

It has one property named "myNestedModel" which has the following definition:

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

      

It also has one property name 'myModel'. Now if I create an instance of MyModel:

aModel = new MyModel ();

The nested model will be set in the initialization method MyModel. Then I 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 great and I get the JSON representation of MyModel and its MyNestedModel property. The problem occurs when I use the default values ​​like:

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

      

This causes a problem with JSON.stringify as it does not support circular references. I am assuming a circular reference is being created because all MyModel instances have the same MyNestedModel instance. Whereas the initialize method creates a new nested model for each instance.

Questions:

  • Is my understanding of the defaults:{}

    "cause" of the problem correct?
  • From a question I posted recently , I got I need to use default values ​​for all properties. If this is the case, how should I use the defaults in the presented scenario in this post / question?
  • Can anyone clarify the usage defaults:{}

      with regard to when the value is applied, when is it overridden and whether the instances have the same default "instances"?
+3


source to share


1 answer


By default, only the attributes inside your model (data in the model) are used, and whenever you create your model, it takes on the defaults and sets the attributes. eg.

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 will print when you call toJSON

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

      

Since the defaults are an object, every value you put there is evaluated immediately like this:

defaults : {
    rating : defaultRating()
}

      

will call defaultRating () - not every time you initialize the model, but immediately (in an extension method)

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



In your example, you have the following errors:

1.set value without property

 defaults : {
      PROPERTY : new Model() 
 }

      

2. You don't need this option for your default settings - you should only put the attributes (data) for the model there

The default is always applied until it is overridden by new defaults in the extended model, for example.

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

      

now your myModel when initialized will be

 { beta : 'gama' } // alpha : 'beta' will not be set as value, because it is replaced

      

+7


source







All Articles