Exercise in Callbacks [#bestpractices]

3 views
Skip to first unread message

jiggliemon

unread,
Jun 25, 2010, 1:34:26 PM6/25/10
to MooTools Users
I'm having a hard time wrapping my head around the practice of
Callbacks. Arron rewrote one of my examples using callbacks, and no
matter how much I search for a good description of "How and Why" to
use them, I'm always left empty. So why not take it to the smartest
people in Javascript?

I wrote a little fiddle of two functions that I would like to chain,
But due to the asynchronous nature of "Ajax", my second function is
fired before prior to the first Function's completion.

I realize in the current example. I could simply attache function 2 to
the Asset.images's #onComplete method, but that's going to defeat the
purpose of my little exercise - using callbacks.

Any help in executing a simple "Callback" method, would be awesome.

Thanks dudes!

http://jsfiddle.net/mvmvp/

Ryan Florence

unread,
Jun 25, 2010, 2:02:42 PM6/25/10
to mootool...@googlegroups.com
I'm not clear about what you're asking.

Callbacks, or events, are simply functions that you can call when stuff happens.

Can you ask a specific question? Your demo looks like you get the gist of it.

Paul Saukas

unread,
Jun 25, 2010, 2:17:13 PM6/25/10
to mootool...@googlegroups.com

I believe your looking for this . http://jsfiddle.net/SA2kQ/

A callback by my understanding (self taught so be warned) is a function B called on completion of another function A from within Function A . 

Thus the move of afterImagesLoaded(); from the domready to the onComplete of getImages().

Paul Saukas

unread,
Jun 25, 2010, 2:19:35 PM6/25/10
to mootool...@googlegroups.com
good god that was typed to fast :) correction to basic English .



A callback by my understanding (self taught so be warned) is
 
Function B called on completion of Function A from within Function A . 

nwhite

unread,
Jun 25, 2010, 2:48:47 PM6/25/10
to mootool...@googlegroups.com
On Fri, Jun 25, 2010 at 11:19 AM, Paul Saukas <araso...@gmail.com> wrote:
good god that was typed to fast :) correction to basic English .



A callback by my understanding (self taught so be warned) is
 
Function B called on completion of Function A from within Function A . 


Whereas Function B is an argument passed to Function A.

Both examples are using callbacks... domready, function(){..} is a callback, all the 'on' functions in Assets.images are callbacks that have been abstracted to an interface. However the way 'afterImagesLoaded' and 'updateProgress' are called are not true callbacks.

Paul Saukas

unread,
Jun 25, 2010, 2:59:56 PM6/25/10
to mootool...@googlegroups.com
Thanks!  Now I have a better/correct understanding as well :D

jiggliemon

unread,
Jun 25, 2010, 5:04:15 PM6/25/10
to MooTools Users
http://jsfiddle.net/mvmvp/2/
This is Essentially what I would like to happen.

var Getter = new getImages();

// Check ig get Imates is complete.
//Is get Images complete?
//If so, move on, if not... check back later.
while(Getter.state == false) { wait a second.... then check again }

Now fire afterImagesLoaded();



On Jun 25, 11:48 am, nwhite <changereal...@gmail.com> wrote:
> On Fri, Jun 25, 2010 at 11:19 AM, Paul Saukas <arasoihe...@gmail.com> wrote:
> > good god that was typed to fast :) correction to basic English .
>
> > A callback by my understanding (self taught so be warned) is
>
> > Function B called on completion of Function A from within Function A .
>
> Whereas Function B is an argument passed to Function A.
>
> Both examples are using callbacks... domready, function(){..} is a callback,
> all the 'on' functions in Assets.images are callbacks that have been
> abstracted to an interface. However the way 'afterImagesLoaded' and
> 'updateProgress' are called are not true callbacks.
>
> http://docs.jquery.com/Types#Callback
>
>
>
>
>
> > On Fri, Jun 25, 2010 at 2:17 PM, Paul Saukas <arasoihe...@gmail.com>wrote:
>
> >> I believe your looking for this .http://jsfiddle.net/SA2kQ/

jiggliemon

unread,
Jun 25, 2010, 6:30:29 PM6/25/10
to MooTools Users
Ok i've come to realize how silly the questions is, and how it's
impossible.
Unless someone has a magical way to pause a particular timeline...

The basis for my thinking was an answer i got from Aaron a while back
regarding a similar timeline issue.
His answer contained callbacks to contain functions that returned what
i was waiting for (that's quite a sentence).

However after a better examination of Aaron's answer proves that it in
no way describes what I was thinking about.

Here's the response that I got a while back that made me think
somewhere in the world of javascript I would be able to work around
they synchronistic/asynchronous issues.
http://paste.mootools.net/f401cfb7c
Reply all
Reply to author
Forward
0 new messages