Guest User

Untitled

a guest
Aug 15th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.93 KB | None | 0 0
  1. When would I use JQuery.Callbacks?
  2. $.callbacks.add("foo", myFunction);
  3.  
  4. $.callbacks().fire("foo", args);
  5.  
  6. .fireWith(context, args)
  7.  
  8. var clickCallbacks = $.Callbacks();
  9.  
  10. clickCallbacks.add(function() { //one one function piece
  11. //parse and do something on the scope of `this`
  12. var c = parseInt(this.text(), 10);
  13. this.text(c + 1);
  14. });
  15. clickCallbacks.add(function(id) { //add a second non-related function piece
  16. //do something with the arguments that were passed
  17. $('span', '#last').text(id);
  18. });
  19.  
  20. $('.click').click(function() {
  21. var $ele = $(this).next('div').find('[id^="clickCount"]');
  22. clickCallbacks.fireWith($ele, [this.id]); //do two separate but related things.
  23. });
  24.  
  25. readyList = jQuery.Callbacks( "once memory" );
  26.  
  27. ready: function( fn ) {
  28. // Attach the listeners
  29. jQuery.bindReady();
  30.  
  31. // Add the callback
  32. readyList.add( fn );
  33.  
  34. return this;
  35. }
  36.  
  37. readyList.fireWith( document, [ jQuery ] );
Add Comment
Please, Sign In to add comment