Well, I had the same problem, so I came up with the following base class to fix my problem.
export class ViewModelBase { private prefix: string = 'On'; public Initialize() { for (var methodName in this) { var fn = this[methodName]; var newMethodName = methodName.substr(this.prefix.length); if (typeof fn === 'function' && methodName.indexOf(this.prefix) == 0 && this[newMethodName] == undefined) { this[newMethodName] = $.proxy(fn, this); } } } }
what it does is a loop of all members of your class, and if the method starts with On, it will create a new method without On, which will call the original method with the correct context.
Not that $.proxy is a jquery call, so it requires jquery.
Peter
source share