Skip to content

Instantly share code, notes, and snippets.

@giuseppeg
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save giuseppeg/2611a50416443bf6d375 to your computer and use it in GitHub Desktop.
Save giuseppeg/2611a50416443bf6d375 to your computer and use it in GitHub Desktop.
Possible fix to FlightJS's on https://github.com/flightjs/flight/issues/260
this.off = function() {
var $element, type, callback;
var lastIndex = arguments.length - 1;
var instanceInfo,
origTypeRegex = '^(type)(?:\.(.+)|)$';
if (typeof arguments[lastIndex] == 'function') {
callback = arguments[lastIndex];
lastIndex -= 1;
}
if (lastIndex == 1) {
$element = $(arguments[0]);
type = arguments[1];
} else {
$element = this.$node;
type = arguments[0];
}
if (callback) {
//this callback may be the original function or a bound version
var boundFunctions = callback.target ? callback.target.bound : callback.bound || [];
//set callback to version bound against this instance
boundFunctions && boundFunctions.some(function(fn, i, arr) {
if (fn.context && (this.identity == fn.context.identity)) {
arr.splice(i, 1);
callback = fn;
return true;
}
}, this);
$element.off(type, callback);
} else {
// Loop through the events of `this` instance
// and unbind using the callback
instanceInfo = registry.findInstanceInfo(this);
if (instanceInfo) {
origTypeRegex = new RegExp(origTypeRegex.replace('type', type), 'i');
instanceInfo.events.forEach(function (event) {
if (origTypeRegex.test(event.type)) {
$element.off(type, event.callback);
}
});
}
}
return $element;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment