반응형
백본에서 상위 클래스에 액세스
오늘처럼 완전히 덮어 쓰는 대신 initialize
상속 된 MyModel
클래스 내부에서 부모 클래스 의 메서드 를 호출해야합니다 .
어떻게 할 수 있습니까?
지금 내 코드는 다음과 같습니다.
BaseModel = Backbone.Model.extend({
initialize: function(attributes, options) {
// Do parent stuff stuff
}
});
MyModel = BaseModel.extend({
initialize: function() {
// Invoke BaseModel.initialize();
// Continue doing specific stuff for this child-class.
},
});
MyModel = BaseModel.extend({
initialize: function() {
MyModel.__super__.initialize.apply(this, arguments);
// Continue doing specific stuff for this child-class.
},
});
시험
MyModel = BaseModel.extend({
initialize: function() {
BaseModel.prototype.initialize.apply(this, arguments);
// Continue doing specific stuff for this child-class.
},
});
이것은 내 모델 사이에서 상속하려고 할 때 나를 위해 일했습니다.
MyModel.prototype.initialize.call(this, options);
http://documentcloud.github.com/backbone/#Model-extend 에서 참조
감사.
나는 그것이 될 것이라고 생각한다
MyModel = BaseModel.extend({
initialize: function() {
this.constructor.__super__.initialize.call(this);
// Continue doing specific stuff for this child-class.
},
});
이것은 Backbone 의 Super와 거의 중복되는 것처럼 보이므로 다음과 같은 것을 원합니다.
Backbone.Model.prototype.initialize.call(this);
@wheresrhys와 비슷하지만 BaseModel.initialize가 인수를 예상하는 경우 호출 대신 apply를 사용합니다. 초기화시 백본 모델에 전달할 수있는 속성 맵을 처리하지 않으려 고하지만 BaseModel이 실제로보기 또는 컬렉션 인 경우 옵션을 설정하고 싶을 수 있습니다.
var MyModel = BaseModel.extend({
initialize: function() {
this.constructor.__super__.initialize.apply(this, arguments);
// Continue doing specific stuff for this child-class.
},
});
여기에 다중 생성 callSuper 메서드가 있습니다. 확장 클래스에 추가하기 만하면됩니다.
callSuper: function (methodName) {
var previousSuperPrototype, fn, ret;
if (this.currentSuperPrototype) {
previousSuperPrototype = this.currentSuperPrototype;
// Up we go
this.currentSuperPrototype = this.currentSuperPrototype.constructor.__super__;
} else {
// First level, just to to the parent
this.currentSuperPrototype = this.constructor.__super__;
previousSuperPrototype = null;
}
fn = this.currentSuperPrototype[methodName];
ret = (arguments.length > 1) ? fn.apply(this, Array.prototype.slice.call(arguments, 1)) : fn.call(this);
this.currentSuperPrototype = previousSuperPrototype;
return ret;
}
함수 상속을 사용하여 코드를 다시 작성하는 것을 고려할 수 있습니다.
var BackBone=function(){
var that={};
that.m1=function(){
};
return that;
};
var MyModel=function(){
var that=BackBone();
var original_m1=that.m1;
//overriding of m1
that.m1=function(){
//call original m1
original_m1();
//custom code for m1
};
};
참고 URL : https://stackoverflow.com/questions/8970606/accessing-parent-class-in-backbone
반응형
'Development Tip' 카테고리의 다른 글
HTML에서 공백없이 긴 줄을 감싸는 방법은 무엇입니까? (0) | 2020.11.09 |
---|---|
PHP 경고 : 알 수 없음 : 스트림을 열지 못했습니다. (0) | 2020.11.09 |
R dplyr : 여러 열 삭제 (0) | 2020.11.09 |
DataGridView에 선택한 행이 표시되도록하려면 어떻게해야합니까? (0) | 2020.11.09 |
.translate ()를 사용하여 Python 3.x의 문자열에서 구두점을 제거하는 방법은 무엇입니까? (0) | 2020.11.09 |