devwannabe
devwannabe

Reputation: 3210

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 })

Upvotes: 0

Views: 44

Answers (2)

devwannabe
devwannabe

Reputation: 3210

I used your solution plus the parse was required but I had to include {parse:true} since I was instantiating it.

parse(resp) {
  _.extend(resp.properties, this.defaults.properties);
  return resp
}

Upvotes: 0

djaszczurowski
djaszczurowski

Reputation: 4515

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 })

Upvotes: 1

Related Questions