3

所以我有这个现状:

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(),但要为其添加额外的功能,我该怎么做呢?

4

1 回答 1

8

实现这一点的两种方法:您可以通过在基类中创建“渲染挂钩”来添加对覆盖行为的显式支持,或者您必须从超类方法调用覆盖的基方法:

在基类中渲染钩子:

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);
  }
});
于 2013-07-03T09:20:04.497 回答