将值设置为 Backbone.Model 中的折叠默认值
Set values to the folded defaults in Backbone.Model
我在默认值中有一个这样的对象:
app.MultiwidgetModel = Backbone.Model.extend({
defaults : {
....
operatorData : {
helloBlock : null,
greeting : null,
avatarBlock : null,
avatarImg : null
},
....
},
initialize: function(){
....
}
});
如何在初始化函数中为 operatorData 内部属性(helloBlock、greeting)等设置值?如果可能,我应该使用什么语法?
如果您只想在初始化中为当前模型设置值并触发适当的 change
事件,这应该可行:
app.MultiwidgetModel = Backbone.Model.extend({
defaults : {
....
operatorData : {
helloBlock : null,
greeting : null,
avatarBlock : null,
avatarImg : null
},
....
},
initialize: function(){
var opData = this.get('operatorData');
opData.helloBlock = 'foo';
opData.greeting = 'bar';
...
this.set({operatorData: opData}); // Without this, the data will change but you won't have a change:operatorData event triggered
}
});
如果您想编辑默认值,您应该可以使用
app.MultiwidgetModel.prototype.defaults.operatorData = {
helloBlock : 'foo',
greeting : 'bar'
}
这是你想要的吗?
我在默认值中有一个这样的对象:
app.MultiwidgetModel = Backbone.Model.extend({
defaults : {
....
operatorData : {
helloBlock : null,
greeting : null,
avatarBlock : null,
avatarImg : null
},
....
},
initialize: function(){
....
}
});
如何在初始化函数中为 operatorData 内部属性(helloBlock、greeting)等设置值?如果可能,我应该使用什么语法?
如果您只想在初始化中为当前模型设置值并触发适当的 change
事件,这应该可行:
app.MultiwidgetModel = Backbone.Model.extend({
defaults : {
....
operatorData : {
helloBlock : null,
greeting : null,
avatarBlock : null,
avatarImg : null
},
....
},
initialize: function(){
var opData = this.get('operatorData');
opData.helloBlock = 'foo';
opData.greeting = 'bar';
...
this.set({operatorData: opData}); // Without this, the data will change but you won't have a change:operatorData event triggered
}
});
如果您想编辑默认值,您应该可以使用
app.MultiwidgetModel.prototype.defaults.operatorData = {
helloBlock : 'foo',
greeting : 'bar'
}
这是你想要的吗?