Backbone Models: defaults based on a condition

I’m pretty new to BackboneJS, looking for some help in the defaulting an attribute based on a condition How can I default name to TEST based on a condition that is only if model.attribute = ‘xyz’ in Backbone models.

defaults: function() {
  return {
   name: 'TEST'
  }
}

Thanks

Answer

You set defaults when you extend a Backbone model so you don’t really have access to any instance attributes there.

If the default values you want to change depend on instance modal attributes set on instatiation (i.e new MyModel({foo:"bar"})) when you’d be better of setting an initialize() callback when extending your model.

var MyModel = Backbone.Model.extend({
  initialize: function(options) {
    // I have access to this.attributes here
  }
});