Last active
December 20, 2015 13:29
-
-
Save ssafejava/6139139 to your computer and use it in GitHub Desktop.
Throw errors when binding null events in Backbone.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*global Backbone: true, _:true */ | |
(function() { | |
"use strict"; | |
/** | |
* Backbone.eventSafety | |
* | |
* Simple plugin that throws an error when Backbone.Events.on & Backbone.Events.once are called | |
* without a callback. Because you might be counting on that and accidentally pass it undefined | |
* because of a scoping error. | |
* Why Backbone doesn't do that, only God and @jashkenas knows. | |
* | |
* @author ssafejava | |
*/ | |
var oldOn = Backbone.Events.on, oldOnce = Backbone.Events.once; | |
var override = { | |
on: function(name, callback, context){ | |
if (!callback) throw new Error("A callback must be provided to on() when binding an event!"); | |
return oldOn.apply(this, arguments); | |
}, | |
once: function(name, callback, context){ | |
if (!callback) throw new Error("A callback must be provided to once() when binding an event!"); | |
return oldOnce.apply(this, arguments); | |
} | |
}; | |
// Backward compatibility | |
override.bind = override.on; | |
_.extend(Backbone, override); | |
_.extend(Backbone.Collection.prototype, override); | |
_.extend(Backbone.Events, override); | |
_.extend(Backbone.History.prototype, override); | |
_.extend(Backbone.Model.prototype, override); | |
_.extend(Backbone.Router.prototype, override); | |
_.extend(Backbone.View.prototype, override); | |
}).call(this); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment