admin管理员组文章数量:1318156
I have a view:
var MultiSelectCompositeView = Backbone.Marionette.CompositeView.extend({
events: {
'click .listItem': 'setSelectedOnClick'
},
onRender: function () {
console.log("MultiSelectCompositeView onRender has executed.");
}
});
and I have another view which extends MultiSelectCompositeView:
var VideoSearchView = MultiSelectCompositeView.extend({
events: _.extend(MultiSelectCompositeView.prototype.events, {
'input @ui.searchInput': 'showVideoSuggestions',
'click button#hideVideoSearch': 'destroyModel',
'contextmenu @ui.videoSearchResults': 'showContextMenu'
},
onRender: function () {
this.ui.playlistActions.append((new PlaySelectedButtonView()).render().el);
this.ui.playlistActions.append((new SaveSelectedButtonView()).render().el);
// TODO: Is there a better way to do this?
MultiSelectCompositeView.prototype.onRender.call(this, arguments);
}
});
I'm unhappy with the fact that VideoSearchView doesn't implicitly extend the events of MultiSelectCompositeView and that VideoSearchView has to manually call MultiSelectCompositeView's onRender method.
Is there something with Backbone.Marionette which would allow me to extend my custom view in a more seamless fashion?
I have a view:
var MultiSelectCompositeView = Backbone.Marionette.CompositeView.extend({
events: {
'click .listItem': 'setSelectedOnClick'
},
onRender: function () {
console.log("MultiSelectCompositeView onRender has executed.");
}
});
and I have another view which extends MultiSelectCompositeView:
var VideoSearchView = MultiSelectCompositeView.extend({
events: _.extend(MultiSelectCompositeView.prototype.events, {
'input @ui.searchInput': 'showVideoSuggestions',
'click button#hideVideoSearch': 'destroyModel',
'contextmenu @ui.videoSearchResults': 'showContextMenu'
},
onRender: function () {
this.ui.playlistActions.append((new PlaySelectedButtonView()).render().el);
this.ui.playlistActions.append((new SaveSelectedButtonView()).render().el);
// TODO: Is there a better way to do this?
MultiSelectCompositeView.prototype.onRender.call(this, arguments);
}
});
I'm unhappy with the fact that VideoSearchView doesn't implicitly extend the events of MultiSelectCompositeView and that VideoSearchView has to manually call MultiSelectCompositeView's onRender method.
Is there something with Backbone.Marionette which would allow me to extend my custom view in a more seamless fashion?
Share Improve this question asked Feb 1, 2014 at 21:55 Sean AndersonSean Anderson 29.4k33 gold badges132 silver badges242 bronze badges2 Answers
Reset to default 8You have done two things here in the wrong way.
First. Do not change the prototype of the MultiSelectCompositeView, just create and use a new object:
events : _.extend({}, MultiSelectCompositeView.prototype.events, {
'input @ui.searchInput' : 'showVideoSuggestions'
// ...
})
Second. Execute super method with proper arguments (using apply
not call
):
onRender : function () {
// ...
MultiSelectCompositeView.prototype.onRender.apply(this, arguments);
}
Or you can use "shortcut":
var superProto = MultiSelectCompositeView.prototype;
var VideoSearchView = MultiSelectCompositeView.extend({
onRender : function () {
// ...
superProto.onRender.apply(this, arguments);
}
});
See also - Super in Backbone.
Not as part of marionette/backbone... I know, it is a pain.
If you use coffeescript you can use the super keyword
onRender: function () {
this.ui.playlistActions.append((new PlaySelectedButtonView()).render().el);
this.ui.playlistActions.append((new SaveSelectedButtonView()).render().el);
super # this will automatically pass the arguments - notice no parentheses
}
As for your events extension you can use the ::
events: _.extend({}, @::events, # rest of code
本文标签:
版权声明:本文标题:javascript - Extending a custom Backbone.Marionette view. How to implicitly incur prototype's eventsonRender? - Stack Ov 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742042216a2417600.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论