Backbone.js 主干继承,合并render()函数
所以我现在的情况是:Backbone.js 主干继承,合并render()函数,backbone.js,Backbone.js,所以我现在的情况是: app.Ui.ModalView = Backbone.View.extend({ events: { }, initialize: function() { }, render: function() { var that = this; var model = this.model.toJSON(); that.$el.html(that.template(_.extend
app.Ui.ModalView = Backbone.View.extend({
events: {
},
initialize: function() {
},
render: function() {
var that = this;
var model = this.model.toJSON();
that.$el.html(that.template(_.extend(this.params || {}, {
model: model,
})));
return this;
}
});
然后是继承的视图:
app.Views.childView = kf.Ui.ModalView.extend({
template: JST["templates/app/blah/blah-edit.html"],
events: {
},
initialize: function() {
var that = this;
this.events = _.extend({}, app.Ui.ModalView.prototype.events, this.events);
app.Ui.ModalView.prototype.initialize.apply(this, arguments);
},
render: function(){
// add extra logic in this render function, to run as well as the inherited render function?
}
});
因此,我不想覆盖父级的
render()
,但要向它添加额外的功能,我该如何做呢?有两种方法可以实现这一点:要么通过在基类中创建“render hook”来添加显式支持来覆盖行为,或者,您必须从超类方法调用重写的基方法:
基类中的渲染挂钩:
app.Ui.ModalView = Backbone.View.extend({
render: function() {
//if this instance (superclass) defines an `onRender` method, call it
if(this.onRender) this.onRender();
//...other view code
}
}
app.Views.childView = kf.Ui.ModalView.extend({
onRender: function() {
//your custom code here
}
});
app.Views.childView = kf.Ui.ModalView.extend({
render: function() {
//your custom code here
//call the base class `render` method
kf.Ui.ModalView.prototype.render.apply(this, arguments);
}
});
从超类调用基类方法:
app.Ui.ModalView = Backbone.View.extend({
render: function() {
//if this instance (superclass) defines an `onRender` method, call it
if(this.onRender) this.onRender();
//...other view code
}
}
app.Views.childView = kf.Ui.ModalView.extend({
onRender: function() {
//your custom code here
}
});
app.Views.childView = kf.Ui.ModalView.extend({
render: function() {
//your custom code here
//call the base class `render` method
kf.Ui.ModalView.prototype.render.apply(this, arguments);
}
});
@尼可什,哎呀,修好了。问题是我有时打字比我想象的快。谢谢。你也可以使用主干网访问父类的方式:
this.construtor.\uuuu super\uuuu
@Loamhoof似乎不推荐使用\uuuu super\uuuu
:(我在阅读时发现)