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

これを実現するには 2 つの方法があります。基本クラスに「レンダー フック」を作成して動作をオーバーライドするための明示的なサポートを追加するか、オーバーライドされた基本メソッドをスーパークラス メソッドから呼び出す必要があります。

基本クラスのレンダリング フック:

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 に答える