为主干模型中的嵌套属性指定值,但不使用.set方法

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

本文关键字:方法 set 模型 属性 嵌套      更新时间:2023-09-26

例如,我的默认值是

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

我喜欢在properties下为weight属性赋值,但不使用.set。我想在实例化时这样做。

这是正确的吗?

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

不幸的是,它不起作用,您必须将属性作为对象传递。例如:

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

我使用了您的解决方案,并且需要解析,但由于我正在实例化它,所以我必须包含{parse:true}。

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