I'm not ordinary Javascript, but I dive in, read the book by Douglas Crockford and write some trivial, useful tidbits like the Chrome and Node.js extensions (note that this question does not apply to any of them).
I'm currently trying to figure out how to save a reference to the object that triggers the AJAX request, that is: after I installed the onload event handler (this is from inside the Chrome extension, so I'm using the XMLHttpRequest base object), is there a way with through which I can return to MyObject in the following example:
MyObject.prototype = { PerformAction: function() { this.Request = new XMLHttpRequest(); this.Request.open("GET", this.ConstructUrl(), true);
Doing this exactly means that this will be the object of the request, and if I just present the shell:
this.Request.onload = function() { ActionResultHandler() };
well, that is simply not going to do anything, because ActionResultHandler is now beyond the scope. The reason I'm asking here is because I have discovered trivial cases of manipulating callers (e.g. manipulating what this means inside a function), but assuming that OO-ified Javascript and AJAX are literally everywhere, this should be a famous, simple problem, but my google fu can't get me here. In C #, events are triggered in the context of who attaches to them, and not the object that triggers the event, so this does not occur daily. Maybe there is a much better JS template that completely avoids this problem?
javascript ajax design-patterns
Marc Bollinger Jul 07 '10 at 7:45 a.m. 2010-07-07 19:45
source share