我使用主干骨干时的两种常见情况:
属性列为默认值,然后设置
modelExample_A: Backbone.Model.extend({
defaults: {
whatever: 'foo'
something: 'blah'
}
});
viewExample_A: Backbone.View.extend({
//our view definition
});
var Example_A = new viewExample_A({
model: new modelExample_A()
})
Example_A.set({
'whatever': 'bar',
'something': 'weeeeeee',
});
属性未列为默认值,然后设置
modelExample_A: Backbone.Model.extend({
});
viewExample_A: Backbone.View.extend({
//our view definition
});
var Example_A = new viewExample_A({
model: new modelExample_A()
})
Example_A.set({
'whatever': 'bar',
'something': 'weeeeeee',
});
属性未列为默认值,在创建时设置
modelExample_A: Backbone.Model.extend({
});
viewExample_A: Backbone.View.extend({
//our view definition
});
var Example_A = new viewExample_A({
model: new modelExample_A({
'whatever': 'bar',
'something': 'weeeeeee',
})
})
但是我想设置模型属性的情况呢?我知道这通常是不鼓励的,但有时在我的代码中我喜欢不知道什么模型是当前模型的父模型。这是几乎可以肯定永远不会改变的东西,因此没有理由将属性放入事件侦听/onChange 目的。此外,这是没有默认值的东西(它只能在上下文中获取一个值),那么将它设置为模型的属性是否可以?或者这会导致问题吗?
设置属性而不是属性
modelExample_A: Backbone.Model.extend({
defaults: {
whatever: 'foo'
something: 'blah'
}
});
viewExample_A: Backbone.View.extend({
//our view definition
});
var Example_A = new viewExample_A({
model: new modelExample_A({
'whatever': 'bar',
'something': 'weeeeeee',
})
})
Example_A.parentModel = parentModelExample;