AJAX Scope in Prototype.JS #47
thecodedrift
announced in
Thunked
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I haven't seen this covered much, but I really felt it was worth a mention. A lot of people have worked with the prototype library, including myself. One of the most challenging things I've had to do was create an object that could make an AJAX request, and then internalize the response. Calling a function out on the global scope is messy and in poor taste if you can avoid it. I thought back to my Java classes, and remember an almost reflexive style of programming. About an hour later (including reading through prototype's source) I had a working model. The major players in this design are the
XMLHttpCatchobject
and theAjax.Request
object. Our goal is to call a sending function inside of an instance ofmyObj
, and then have the response be ran in the context ofmyObj
. By default, you cannot refer tothis
inside of the callback, because of scope issues. The solution is to encapsulate the request, and then have a way for the response to find its way back home. We do this by passing the scope down into theXMLHttpCatch object
. Hopefully other people will find this useful, as occasionally it is not practical to have globally scoped functions foronComplete
, and you want access to all the properthis.methodName()
calls. I thought about moving the catch object intomyObj
as a function definition during initialize, but the current model is a bit more reusable.Beta Was this translation helpful? Give feedback.
All reactions