Typescript and jQuery-Events

Topics: Code Checkins, General, Language Specification
Mar 14, 2014 at 7:13 AM
Edited Mar 14, 2014 at 7:14 AM
Hi all,

when I do this in my Typescript class:
    public Initialize(): void {
        $(this.SelectorId).draggable({
            axis: "y",
            scroll: false,
            containment: "window",
            stop: function (event, ui) {
                this.OnResize.trigger("stop", ui);
            }
        });
    }
I receive the Event, but this doesn't point to the class instance it points to the jQuery object which is in this case a h3-Element.

How can I access the class members?

Best regards,
Mike
Coordinator
Mar 14, 2014 at 3:39 PM
How 'this' works is a bit different in JavaScript (and TypeScript by it being a superset). You probably need to turn the function pointed to by "stop:" to a lambda. You can read about this here: https://typescript.codeplex.com/wikipage?title=Functions%20in%20TypeScript&referringTitle=TypeScript%20Documentation (scroll down to Lambdas and using 'this')

Another good reference is Yehuda Katz's blog post on 'this': http://yehudakatz.com/2011/08/11/understanding-javascript-function-invocation-and-this/
Marked as answer by NCC1701M on 3/16/2014 at 11:07 PM
Mar 17, 2014 at 6:07 AM
Hi,

thanks for your help! :-)

Best regards,
Mike