forked from steinitz-zz/Meteor-React-ES6-AutoBind
-
Notifications
You must be signed in to change notification settings - Fork 0
/
react-es6-autobind.js
37 lines (34 loc) · 897 Bytes
/
react-es6-autobind.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// Write your package code here!
SteinitzREA = {};
SteinitzREA.getMethodsForInstance = function getMethodsForInstance (instance)
{
var result = [];
for (var methodName in instance)
{
if ("function" == typeof instance [methodName])
{
result.push (methodName)
}
}
return result;
};
// bind all object method contexts to the instance - i.e. this = instance
SteinitzREA.AutoBind = function AutoBind (instance)
{
methods = SteinitzREA.getMethodsForInstance (instance);
// Object.keys (instance.constructor.prototype).forEach (
methods.forEach (
function (aMethod)
{
try
{
instance [aMethod] = instance.constructor.prototype [aMethod].bind (instance);
// console.log ("DT.Autobind - binding method " + aMethod);
}
catch (theException)
{
console.log ("DT.Autobind - error binding method " + aMethod + ", error: " + theException.message);
}
}
);
};