Search code examples
javascriptbackbone.js

Assign a value to a nested property in a backbone model but not using .set method


For example, my defaults is

defaults: { name: '',
  properties: {
    weight: 0
  }
}

I like to assign to the weight property under properties but without using .set. I'd like to do it during instantiation.

Is this correct?

this.model = new MyModel({name: 'Kenny Rogers', properties.weight: 195 })

Solution

  • unfortunately it won't work, you have to pass properties as an object. For instance:

    this.model = new MyModel({name: 'Kenny Rogers', properties: { weight: 195 })