"Arnar Birgisson" <[EMAIL PROTECTED]> writes:

> Hi there,

Hi Arnar.

> I believe you are on the right track. You should be able to get a
> deferred by calling loadJSONDoc and adding your callbacks:

I can't do that.  I have to call two different "loadJSONDoc"s, one in each
callback to build some DOM and swap what I have on HTML while in the third
callback.

> var d = loadJSONDoc('someurl');
> d.addCallback(func1);
> d.addCallback(func1);
> d.addCallback(processResults);
>
> function func1 (x) {
>   // do some processing, calculate y
>   return y;
> }
>
> function func2 (y) {
>   // do some calculation, find z
>   return [y, z];
> }
>
> function processResults(result) {
>   y = result[0];
>   z = result[1];
>   // do something with y and z
> }

My case is more like:

d = new Deferred()
d.addCallback(func1);
d.addCallback(func1);
d.addCallback(processResults);

function func1 (x) {
  var d = loadJSONDoc('someurl');
  // do some processing, calculate y
  return y;
}

function func2 (y) {
  var d = loadJSONDoc('someurl');
  // do some calculation, find z
  return [y, z];
}

function processResults(result) {
  y = result[0];
  z = result[1];
  // do something with y and z
}

I tried this approach, but I only got data in "result[0]", with "result[1]"
being an "unfired" callback (this is what I got from the log messages.

> Once the JSON request is loaded, func1 is called with the result
> (already evaluated to a Javascript object, no need for
> evalJSONRequest), func2 is called with the result of func1 - which

(I'm not using loadJSONDoc.  It didn't work here while the normal
getXMLHttpRequest did...  So, I still have to evalJSONRequest :-()

> combines it with its own result. processResults is then called with
> the result of func2.

This is what I tried but didn't work...

> There's no need for globals. Seems to me you are using three callbacks
> to deferred, using the first one to initiate the AJAX request.

Nope.  I use three callbacks using the first two to initiate two different
AJAX requests.

> The problem you have I think is here:
>  var d = new Deferred();
>  d.addCallback(get_amostras_analises);
>  d.callback();
>  d.addCallback(get_analises_fator_correcao);
>  d.addCallbacks(monta_tabela_resultados, metadataFetchFailed);
>
> What happens is that when you add the last two callbacks, they get
> called immediately since you already called d.callback() (see
> MochiKit.Async manual).

This is no problem...  What can't happen is the third being called without a
return from both the first and the second AJAX requests.  Since callbacks were
called in the order they were added, I thought they would solve the problem.

> The deferred you return from the first callback is thus not ready when
> it's passed to the second and third one.
>
> If you're simply loading a JSON document, there's no need to create
> the Deferred yourself, just use loadJSONDoc(url) which creates and
> returns the Deferred for you. You rarely have to create Deferreds
> yourself at all - that's what the functions in Async are for
> (loadJSONDoc, callLater, wait, etc.)
>
> Hope I'm understanding your problem correctly :o)
>
> I found Deferred confusing at first since you "addCallbacks" _after_
> you initiate the request (like with loadJSONDoc). The important thing
> to remember is that Javascript events are processed in loops, so you
> Deferred won't change it's state until the next event-loop - so you
> have ample time to add callbacks.

If I could add a callback to the next function while in the function, then
maybe I could get what I want, right?

What I want?  It might be described byt the following "pseudocode" sequence: 

     result1 = doAjax1()
     result2 = doAjax2()
     func3(result1, result1)

I just need the results when the third callback (func3 here) is called. 

-- 
Jorge Godoy      <[EMAIL PROTECTED]>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"MochiKit" 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/mochikit
-~----------~----~----~----~------~----~------~--~---

Reply via email to