I tend to use callbacks when I want something to happen after a certain process completes, or to tie two class instances together/give them references to one another, but I don't really care if any other objects know about it. A good example would be instantiating a class that creates a Faye client to talk to a nodeJS server, and then including a callback to the initialize function to tie it to my ChatClient class once bidirectional communication was established. (This is not necessarily an optimal design, however).
Using the same example, I would attach events to the ChatClient class to let me know when I had recieved new messages, and events to the UI elements (probably classes themselves) which would call functions on the ChatClient class for sending messages. I tend to look at callbacks as "once but never again", and events as "I'm going to be doing this far more than once" On Tue, Jan 4, 2011 at 9:39 AM, אריה גלזר <[email protected]> wrote: > There are 2 questions you need to ask yourself: > 1. Is it productive to have the possibility of multiple callbacks or is it > a good idea to limit it to one callback? > 2. Is the function an integral part of the behavior or is it something that > has no affect on the methods behavior? > > Events allows the user to hook to certain behaviors of your class and > attach extra behavior to them. For example, if I have a Class that does some > effect, I might choose to supply an Options parameter to override that > effect. Since the effect is integral, it's important that it will happen > once and that I will _know_ that it happened (events may or may not be > attached). However, I might also choose to supply a 'done' event, so users > of the class will know that the effect is done, and that they can now do > something (maybe with the result of the operation). > > An example of a place where I used callbacks is my YADMM class( > http://mootools.net/forge/p/yaddm) where the menu opening function is an > option, whereas the same action will also fire an 'open'/'close' event, as > well as attach relevant classes to the elements. > > As a side note - your question should be phrased a little differently - a > callback implies that the function is simply a way for you to call back > outside code - which is exactly what events are for. Not every anonymous > function we pass to our classes is a callback. > > > On Tue, Jan 4, 2011 at 4:07 PM, Robert <[email protected]> wrote: > >> Hi Clement, >> rowvalidator is a validator, so it has to have some way to stop >> execution if something is wrong. I can use chaining, another >> parameter - or perhaps events are not best for this - some kind of >> filter or decorator would be better. Anyway, it was only an example. >> >> I think events may be better for extending classes - but I haven't >> reach that far - yet. ;-) >> >> >> Thanks, >> >> On Jan 4, 2:51 pm, Clement Hallet <[email protected]> wrote: >> > Hi Robert, >> > >> > In my mind, events don't replace callbacks, it's just an other way to >> use them. >> > However it's a more "dynamic" way : you can add (one or more) and remove >> events callbacks whenever you want. >> > >> > I modified your code to use eventshttp://jsfiddle.net/Jxtuk/2/ >> > >> > I'm not sure about what "rowvalidator" does, is its return value used >> inside your "fun" function ? >> > If it is, i'm not sure you can use events, since the callbacks returns >> are not transmit through"fireEvent" (and it couldn't do that, since it may >> call several callbacks, each one having its own return value). >> > >> > Hope what i told is clear enough (my first message in that mailing ^^) >> > >> > Clément. >> > >> > Le 4 janv. 2011 à 14:20, Robert a écrit : >> > >> > >> > >> > >> > >> > >> > >> > > hey masters of moo! >> > >> > > I am trying to make my code more "mooish". If I understand properly, >> > > it means using events in exchange of callbacks. >> > > But I am not sure how to do this and what is the benefit of using >> > > events? >> > >> > > For example I have class for tabular data editing, where I can add >> > > some validation (pseudocode): >> > >> > >http://jsfiddle.net/htZAC/ >> > >> > > Does it make sense to change callback for fireEvent? And how should I >> > > attach it (where)? During creation of object? >> > >> > > Thanks, >> > > btw. is there any other guide to events in moo except for >> > >http://ryanflorence.com/mootools-events/? >> > > > > -- > Arieh Glazer > אריה גלזר > 052-5348-561 > http://www.arieh.co.il > http://www.link-wd.co.il > >
