在marionette中,我如何在视图的父对象上调用相同名称的函数而不覆盖原始函数?

例如:

var someView = new Backbone.Marionette.ItemView.extend({
    onRender: function () {
        console.log('foo');
    }
});

var anotherView = someView.extend({
    onRender: function () {

        // call someView's original onRender function

        console.log('bar');
    }
});

anotherView.render();
.

导致控制台输出:

foo
bar
.

有帮助吗?

解决方案

您可以使用 __super__

var anotherView = someView.extend({
    onRender: function () {
        this.__super__.onRender.call(this);
        console.log('bar');
    }
});
.

或只是直接引用要在实例上应用的方法:

var anotherView = someView.extend({
    onRender: function () {
        someView.prototype.onRender.call(this);
        console.log('bar');
    }
});
.

有关更多信息,请参阅函数的JavaScript类继承什么生成的generacicetagcode do

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top