最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Call a parent view's function in Marionette.js - Stack Overflow

programmeradmin0浏览0评论

In Marionette, how might I go about calling a function of the same name on the parent object of a view without overwriting the original function?

For instance:

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();

Resulting in the console output:

foo
bar

In Marionette, how might I go about calling a function of the same name on the parent object of a view without overwriting the original function?

For instance:

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();

Resulting in the console output:

foo
bar
Share Improve this question asked Jun 4, 2014 at 1:06 nimhnimh 331 silver badge5 bronze badges 1
  • 1 So you don't mean the parent view (in the tree), but the super view from which you inherited? – Bergi Commented Jun 4, 2014 at 1:22
Add a ment  | 

1 Answer 1

Reset to default 7

You can use __super__, which is set up by extend:

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

or just directly reference the method that you want to apply on your instance:

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

For more information, see Javascript Class Inheritance For Functions and what .call() does.

发布评论

评论列表(0)

  1. 暂无评论