[jQuery] Synchronizing

2006-09-02 Thread Jörn Zaefferer
Hi folks,

is there any way to synchronize calls in javascript without using callbacks?

Example:

doSomething();
reset();
doSomethingElse();

Reset calls some asychronous code and I don't want it to return until 
the call is complete.
The obvious solution is to pass doSomethingElse as a callback for reset, 
but that makes it very difficult in the given scenario.

Any ideas?

-- Jörn

___
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/


Re: [jQuery] Synchronizing

2006-09-02 Thread Franck Marcia
 Hi folks,

 is there any way to synchronize calls in javascript without using callbacks?


You could use a sort of proof of concept I wrote months ago:
http://fmarcia.info/jquery/chain. I'm not sure it'll work with the
latest version of jQuery though.

Franck.

___
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/


Re: [jQuery] Synchronizing

2006-09-02 Thread Dave Benjamin
On Sat, 2 Sep 2006, John Resig wrote:

 Neil Mix has released a library that lets you write Javascript code just 
 like that: http://www.neilmix.com/narrativejs/doc/index.html

Wow, that is really impressive. I've been thinking about different ways to 
approach the sync vs. async barrier. One approach has been to chain 
functions together, which I've described here:

http://dev.bestpartyever.com/2006/08/05/taking-the-pain-out-of-async/

Lately I've been experimenting with Parenscript, which is a 
Lisp-to-JavaScript translator that allows you to write macros; if anyone's 
interested I can post some sequencing macro code I've been working on... 
=)

Dave

___
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/


Re: [jQuery] Synchronizing

2006-09-02 Thread John Resig
 Lately I've been experimenting with Parenscript, which is a
 Lisp-to-JavaScript translator that allows you to write macros; if anyone's
 interested I can post some sequencing macro code I've been working on...

Go ahead! I'm interested :-)

--John

___
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/


Re: [jQuery] Synchronizing

2006-09-02 Thread Dave Benjamin
On Sat, 2 Sep 2006, John Resig wrote:

 Lately I've been experimenting with Parenscript, which is a 
 Lisp-to-JavaScript translator that allows you to write macros; if 
 anyone's interested I can post some sequencing macro code I've been 
 working on...

 Go ahead! I'm interested :-)

Right on.

So, for a simple example, here's a snippet of Parenscript code that 
creates a DIV, puts some text in it, and fades it in, out, and back in 
again:

   (.append #$body (html ((:div :id mydiv
  :style (css-inline :background white))
Hello, world!)))
   (seq
(.fade-in #$div #mydiv slow)
(.fade-out #$div #mydiv slow)
(.fade-in #$div #mydiv fast)))

A few notes about the syntax: The #$ is a reader-macro I wrote for for 
convenience; #$body gets transformed into ($ body). I haven't yet 
decided if this is a good thing or not. ;) In Parenscript, functions that 
start with a . such as .append and .fade-in, above, are treated as 
method calls. Capitalization is translated by Parenscript from Lisp-style 
to JavaScript-style, which is why the jQuery methods end up looking like 
.fade-in. The html and css-inline macros are part of Parenscript, 
and allow you to generate JavaScript that builds HTML and CSS from 
s-expressions.

The sequencing macro is seq. The above seq call gets transformed into 
the following:

   (.fade-in #$div #mydiv slow
  (lambda ()
 (.fade-out #$div #mydiv slow
(lambda ()
   (.fade-in #$div #mydiv fast)

The resulting JavaScript is this:

   function main() {
 $('body').append('div id=mydiv style=' + ('background:white')
  + 'Hello, world!/div');
 $('div #mydiv').fadeIn('slow',
function () {
  $('div #mydiv').fadeOut('slow',
  function () {
$('div 
#mydiv').fadeIn('fast');
  });
});
   }

So, this has similar effects to what I think you're trying to achieve with 
method chaining, but is more general because you are not limited to a 
single jQuery object for the dispatch of each chained operation.

Here's my working definition of seq:

   (defjsmacro seq (rest forms)
 (labels
 ((aux (lst)
   (cond
((null lst) nil)
((and (consp lst) (not (cdr lst))) (car lst))
(t (append (car lst) (list `(lambda () ,(aux (cdr lst)
   (aux forms)))

I'd like to extend it to allow inserting ordinary code in the middle of 
the sequence with a par construct - I'm lifting this terminology out of 
an interesting but obscure concurrent language called Occam:

   http://en.wikipedia.org/wiki/Occam_programming_language

Ironically, in an event-driven model, par is really the default 
behavior. When working with background functions, everything is parallel; 
you have to really work at making things sequential.

Dave

___
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/