Javascript 主干JS访问子数据中的基本模型
我想在主干中创建一个基本模型类,以便我的所有其他模型都扩展这个基本类。在基类中的某个时刻,我希望能够访问扩展类的数据集 例如,假设我的基类是:Javascript 主干JS访问子数据中的基本模型,javascript,backbone.js,Javascript,Backbone.js,我想在主干中创建一个基本模型类,以便我的所有其他模型都扩展这个基本类。在基类中的某个时刻,我希望能够访问扩展类的数据集 例如,假设我的基类是: var baseModel = Backbone.Model.extend({ someFunc: function() { // This guy needs to operate on 'protected' array set my the extending model } }, { protected:
var baseModel = Backbone.Model.extend({
someFunc: function() {
// This guy needs to operate on 'protected' array set my the extending model
}
}, {
protected: [] // this is to be set by the extending model
});
var extendingModel = baseModel.extend({
}, {
protected: ['id', 'username'];
});
这能实现吗?好问题
我建议您研究一下主干的方法,它不同于下划线的扩展
extend中的第二个参数是“static property”,因此您无法使用实例方法访问它
是一把小提琴和工作小提琴,它将提供基本结构
源代码
var BaseModel = Backbone.Model.extend({
someFunc: function() {
// This guy needs to operate on 'protected' array set my the extending model
$('.container').append(JSON.stringify(this.protected || 'something'));
}
}, {
// this is to be set by the extending model
});
var ExtendingModel = BaseModel.extend({
protected: ['id', 'username']
}, {
});
var test = new ExtendingModel();
test.someFunc();
var ExtendingModel2 = BaseModel.extend({
protected: ['anotherProp', 'ABC']
}, {
});
test1 = new ExtendingModel2();
test1.someFunc();
test3 = new BaseModel();
test3.someFunc();