Hi,

> fetchData routine make an ajax request, passes along jsonDataM to the
> handler routine, at which time the responseJSON result is assigned to the
> json parameter.

Remember that it's the *value* of `jsonDataM` that gets bound to the
function, not a reference to the variable. (JavaScript is purely pass-
by-value; the value passed in the case of object references is a
reference to the object, but it's still just a value being passed.) So
if your handler function inside `fetchData` is ending up doing
something like this:

    jsonData = response.responseJSON;

...that's not going to have any effect on `jsonDataM`. All you're
doing there is assigning the value of the `responseJSON` property to
your local argument variable within `fetchData`. It's exactly like
doing this:

var foo = 5;
bar(foo);
alert(foo);
function bar(f) {
   f = 6;
}

The alert will still show "5", not "6". Assigning to `f` doesn't have
any effect on `foo`.

One obvious answer is to have `fetchData` just assign directly to
`jsonDataM`, since it's a global, but I hate to offer an answer
suggesting using global variables (even if you already have them), and
I'm guessing you have a reason for making the destination for the data
an argument you give to `fetchData`.

It's easy to simulate pass-by-reference in JavaScript using object
properties. Instead of having a `jsonDataM` variable, have a variable
that's an object containing a *property* called `jsonDataM`. Pass that
object's reference to `fetchData`, and have `fetchData` write to its
`jsonDataM` property.

Applying that solution to my example above:

var obj = {
    foo: 5
};
bar(obj);
alert(obj.foo);
function bar(o) {
   o.foo = 6;
}

Now it alerts "6".

If I'm completely off-base on the above, first off: Apologies. And
second off: My next guess is that the code isn't handling the fact
that the ajax request is *asynchronous*. But my first guess (since
you've titled this as a question about variable scope) is that it's
the pass-by-value thing.

HTH,
--
T.J. Crowder
Independent Software Engineer
tj / crowder software / com
www / crowder software / com

On Apr 16, 11:18 pm, kstubs <kst...@gmail.com> wrote:
> In the following, assuming jsonDataM is defined top of my script (so scope
> of this variable should be good):
>
> mapqueryform.observe('submit', fetchData.bindAsEventListener(mapqueryform,
> jsonDataM));
>
> fetchData routine make an ajax request, passes along jsonDataM to the
> handler routine, at which time the responseJSON result is assigned to the
> json parameter.  I'm expecting my global jsonDataM to have valid json data,
> but it is just NULL.  
>
> To be clear, here is the function definitions for fetchData and the handler:
>
> function fetchData(e, jsonData) { ... }
> function handleFetch(response, jsonData) { ... }
>
> and in fetchData, here is the onComplete:
> onComplete: function(response) { handleFetch(response, jsonData); }

-- 
You received this message because you are subscribed to the Google Groups 
"Prototype & script.aculo.us" group.
To post to this group, send email to prototype-scriptaculous@googlegroups.com.
To unsubscribe from this group, send email to 
prototype-scriptaculous+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/prototype-scriptaculous?hl=en.

Reply via email to