I was looking through new stuff added to jQuery 1.7 and I saw they now have jQuery.Callbacks() http://api.jquery.com/jQuery.Callbacks/.
The documentation shows you how to use jQuery.callbacks() but not any applicable examples of when I would want to use them.
It seems you can add/remove callbacks from a callbacks list and you can do jQuery.callbacks().fire(args), but this just fires off ALL of the callbacks in that list. Maybe I am missing something but this doesn't seem very useful.
In my head when I first saw this new functionality I thought you would be able to use it with key/value pairs. Which would then provide a simple way to manage callback functions in a single place in your application. Something like
$.callbacks.add("foo", myFunction);
and then for example if I wanted to call that callback at the end of my function I could do something like
$.callbacks().fire("foo", args);
However it doesn't look like you can fire off specific callbacks, you can only fire off all of them with the given arguments or none of them.
The closest thing I saw was being given the ability to give the .fire() function a context to set the "this" property
.fireWith(context, args)
but this doesn't really help much either.
Am I misunderstanding the documentation?
If this is the desired functionality what are some applicable examples where this is useful.
var callbacks = $.Callbacks();
. Then you can add methods usingcallbacks.add(myFunction);
(you don't name them, you just add functions to a list). They are then fired (in order) bycallbacks.fire(args)
.fireWith
is used to set a context (this
), but not select which callback to fire.fireWith
also requires the arguments be an array, for example:callbacks.fireWith(document, "foo");
. – Nedanedda