Hi there,
2006/3/31, Jorge Godoy <[EMAIL PROTECTED]>:
> I believe that if I could somehow "extend" the return of the second callback
> with the return of the first one it would solve my problem (there's no
> conflict of property names).
I believe you are on the right track. You should be able to get a
deferred by calling loadJSONDoc and adding your callbacks:
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
}
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
combines it with its own result. processResults is then called with
the result of func2.
> What should I do? Is it possible like this? Should I try using global
> variables to solve that? I want the result of the first two callbacks on the
> third one.
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.
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).
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.
Arnar
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---