Last active
December 31, 2015 18:22
-
-
Save patrixd/8025952 to your computer and use it in GitHub Desktop.
BindAll from underscore that allows 1 argument to bind all the functions from the prototype, or if there are more arguments they will be the only binded
This file contains 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
//bindAll from underscore that allows 1 argument to bind all the functions from the prototype, | |
//or if there are more arguments they will be the only binded | |
_.originalBindAll = _.bindAll; | |
_.bindAll = function (that) { | |
var funcs = Array.prototype.slice.call(arguments, 1), | |
validKeys = [], fn; | |
if (funcs.length == 0) { | |
for (var i in that) { | |
fn = that[i]; | |
if (fn && typeof fn == "function" && (!fn.prototype || _.keys(fn.prototype).length == 0)) | |
validKeys.push(i); | |
} | |
_.originalBindAll.apply(_, [that].concat(validKeys)); | |
} | |
else | |
_.originalBindAll.apply(_, arguments); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment