admin管理员组文章数量:1393121
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
1 Answer
Reset to default 7You 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.
本文标签: javascriptCall a parent view39s function in MarionettejsStack Overflow
版权声明:本文标题:javascript - Call a parent view's function in Marionette.js - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744620361a2615987.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论