我正在尝试使用其他属性设置模型的属性,但我得到undefined
。
这是一个例子:
var TodoItem = Backbone.Model.extend({
defaults: {
status: "incomplete",
relationalStatus: this.status,
},
});
当我尝试使用todoItem.attributes
获取值时。我只是在relationlStatus
值中看到一个空字符串。
为什么没有获得价值?
答案 0 :(得分:0)
您需要在initialize阶段设置此项。 this.status
指的是外部范围,它目前不在模型范围内。
使用初始化的示例。
var TodoItem = Backbone.Model.extend({
defaults: {
status: "incomplete",
relationalStatus: null,
},
initialize: function() {
// Set the relationalStatus to the status property
this.set('relationalStatus', this.get('status'));
}
});