Hi,
Use a closure - if you reference the variable a inside the anonymous function
assigned to the onSuccess callback, the interpreter will preserve a reference
to it. So:
function test() {
var a = 'test';
new Ajax.Request('/myurl',
{ method:'get',
onSuccess: function(transport) {
alert("a = "+a);
}
}
);
}
should work... Note that I just mention 'a' inside the onSuccess fn, no
special syntax is needed.
The same technique works for UI callbacks too - great once you start
programmatically assigning UI event handlers inside objects, rather than the
old onclick attributes and pals.
Closures are a very powerful feature of JavaScript, enabling a much more
fluid style of coding than the pure OO approach. Once you start getting your
head around them, they can't be beat.
HTH
Dave
On Thursday 12 April 2007 11:15, scramatte wrote:
> When I made an Ajax Request, how can I access to parent context
> inside onSuccess callback.
> I mean that in the example below I would like to get value of var
> "a"
----------------------
Author
Ajax in Action http://manning.com/crane
Ajax in Practice http://manning.com/crane2
Prototype & Scriptaculous in Action http://manning.com/crane3
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Spinoffs" group.
To post to this group, send email to [EMAIL PROTECTED]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-spinoffs?hl=en
-~----------~----~----~----~------~----~------~--~---