documentcloud/backbone — Issue #812: allow decorators/wrapper function on view event handlers
Author: clutchski
Created: 2011-12-24 03:34:35+00:00
State: open

Heyo.

What are your thoughts on having a view have an optional wrapper function for it's event handlers. Here's an example that would report any errors unexpected errors in the handler.

var View = Backbone.View.extend({

// the event decorator will take the event handler function as the first argument,
// and any other arguments as the rest
eventDecorator : function () {var func = arguments[0];
var args = Array.prototype.slice(arguments, 1);
try {
f.apply(this, args);
} catch (e) {
reportError(e);
}
});

IMO, the main benefit of this would be allowing easy top-level error handling to js tasks kicked off by DOM events (report to server, show an uh-oh message, whatever). This is generally a very sore-spot in Javascript apps because it requires wrapping every callback function manually, so people just don't do it. But since all Backbone events are bound with delegateEvents, we have a nice place to catch them all with a surgical addition.

What do y'all think?