Yes. You can decorate Backbone.View.constructor
to hook into the View creation lifecycle. You can then register callbacks for any event on all View instance..
!function() {
// Development: log view renders and other events
// Don't execute this function in production.
// Save a reference to the original Backbone.View and create a new type
// that replaces the constructor method
var OriginalView = Backbone.View,
LoggingView = OriginalView.extend({
constructor: function() {
// Execute the original constructor first
OriginalView.apply(this, arguments);
// Allow views to define a `type` property to clarify log messages
var type = this.type || 'Unknown View Type',
cid = this.cid;
// Bind to Marionette.View's `render` event (and any other events)
this.listenTo(this, 'render', function(e,b) {
console.debug('%s %s - %s', type, cid, 'render');
});
}
});
// Replace Backbone.View with our decorated view
Backbone.View = LoggingView;
}();
To log view types, add a property to your View implementations:
MyModule.MyViewType = Marionette.ItemView.extend({
type: 'MyModule.MyViewType',
// ... rest of the view code
});
Reliably determining a JavaScript's object "type" (constructor name) is problematic, so adding this property is the best approach to determine the type of view that is being rendered.
This answer is easily generalised to multiple event types by providing an array of the events you want to be logged:
var events = ['render', 'show', 'beforeClose'];
events.forEach(function(eventType) {
this.listenTo(this, eventType, function() {
console.debug('%s %s - %s', type, cid, eventType)
});
}).bind(this);
Example output:
Projects.ViewType1 view13 - render
Projects.ViewType2 view3 - render
Projects.ViewType3 view6 - render
Projects.ViewType4 view9 - render
Projects.ViewType4 view17 - render
Projects.ViewType4 view19 - render
Projects.ViewType2 view3 - render
This approach solves all of the problems described in the question - there is a a small amount of code, Views don't need altering directly and there is a single function call that can be omitted to remove logging in production code.
This approach is specific to Marionette - because vanilla Backbone's render
method is user-defined there is no equivalent of the render
event.
For more detail on extending Backbone constructor methods see Derick Bailey's blog.