Override Backbone's trigger with one that catches exceptions
Model operations are vulnerable to exceptions thrown by event handlers. Because this can interrupt really important data operations, it's better to let the operation continue and log the error. In all likelihood it's a view-related problem, and that shouldn't cause any data operation to fail. FREEBIE
This commit is contained in:
parent
700272cf85
commit
cc2c3edaa6
4 changed files with 217 additions and 9 deletions
|
@ -1,5 +1,14 @@
|
|||
(function () {
|
||||
// Pure copy from Backbone.
|
||||
// Note: this is all the code required to customize Backbone's trigger() method to make
|
||||
// it resilient to exceptions thrown by event handlers. Indentation and code styles
|
||||
// were kept inline with the Backbone implementation for easier diffs.
|
||||
|
||||
// The changes are:
|
||||
// 1. added 'name' parameter to triggerEvents to give it access to the current event name
|
||||
// 2. added try/catch handlers to triggerEvents with error logging inside every while loop
|
||||
|
||||
// And of course, we update the protoypes of Backbone.Model/Backbone.View as well as
|
||||
// Backbone.Events itself
|
||||
|
||||
// jscs:disable
|
||||
|
||||
|
@ -39,14 +48,62 @@
|
|||
// A difficult-to-believe, but optimized internal dispatch function for
|
||||
// triggering events. Tries to keep the usual cases speedy (most internal
|
||||
// Backbone events have 3 arguments).
|
||||
var triggerEvents = function(events, args) {
|
||||
var triggerEvents = function(events, name, args) {
|
||||
var ev, i = -1, l = events.length, a1 = args[0], a2 = args[1], a3 = args[2];
|
||||
var logError = function(error) {
|
||||
console.log('Model caught error triggering', name, 'event:', error && error.stack ? error.stack : error);
|
||||
};
|
||||
switch (args.length) {
|
||||
case 0: while (++i < l) (ev = events[i]).callback.call(ev.ctx); return;
|
||||
case 1: while (++i < l) (ev = events[i]).callback.call(ev.ctx, a1); return;
|
||||
case 2: while (++i < l) (ev = events[i]).callback.call(ev.ctx, a1, a2); return;
|
||||
case 3: while (++i < l) (ev = events[i]).callback.call(ev.ctx, a1, a2, a3); return;
|
||||
default: while (++i < l) (ev = events[i]).callback.apply(ev.ctx, args); return;
|
||||
case 0:
|
||||
while (++i < l) {
|
||||
try {
|
||||
(ev = events[i]).callback.call(ev.ctx);
|
||||
}
|
||||
catch (error) {
|
||||
logError(error);
|
||||
}
|
||||
}
|
||||
return;
|
||||
case 1:
|
||||
while (++i < l) {
|
||||
try {
|
||||
(ev = events[i]).callback.call(ev.ctx, a1);
|
||||
}
|
||||
catch (error) {
|
||||
logError(error);
|
||||
}
|
||||
}
|
||||
return;
|
||||
case 2:
|
||||
while (++i < l) {
|
||||
try {
|
||||
(ev = events[i]).callback.call(ev.ctx, a1, a2);
|
||||
}
|
||||
catch (error) {
|
||||
logError(error);
|
||||
}
|
||||
}
|
||||
return;
|
||||
case 3:
|
||||
while (++i < l) {
|
||||
try {
|
||||
(ev = events[i]).callback.call(ev.ctx, a1, a2, a3);
|
||||
}
|
||||
catch (error) {
|
||||
logError(error);
|
||||
}
|
||||
}
|
||||
return;
|
||||
default:
|
||||
while (++i < l) {
|
||||
try {
|
||||
(ev = events[i]).callback.apply(ev.ctx, args);
|
||||
}
|
||||
catch (error) {
|
||||
logError(error);
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -60,8 +117,9 @@
|
|||
if (!eventsApi(this, 'trigger', name, args)) return this;
|
||||
var events = this._events[name];
|
||||
var allEvents = this._events.all;
|
||||
if (events) triggerEvents(events, args);
|
||||
if (allEvents) triggerEvents(allEvents, arguments);
|
||||
if (events) triggerEvents(events, name, args);
|
||||
if (allEvents) triggerEvents(allEvents, name, arguments);
|
||||
return this;
|
||||
};
|
||||
})();
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue