Guest User

Untitled

a guest
May 26th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. (function($) {
  2.  
  3. $.fn.thePlugin = function(options) {
  4.  
  5. // build main options before element iteration:
  6. var opts = $.extend({}, $.fn.thePlugin.defaults, options);
  7.  
  8. var $this = $(this);
  9. var thePlugin = {
  10. index: 0,
  11. otherVar: true,
  12.  
  13. init: function() {
  14. if(window.console) window.console.log('init() called. this = ', this);
  15. },
  16.  
  17. doThat: function(){
  18. var a = thePlugin.otherVar;
  19. if(window.console) window.console.log('doThat() called.');
  20. },
  21.  
  22. getIndex: function(){
  23. // the interesting thing here is that from the HTML (public) 'this' will return the api object, not the thePlugin object because that's where it's called from.
  24. if(window.console) window.console.log("internal thePlugin.getIndex() called. this = ", this, "thePlugin = ", thePlugin);
  25.  
  26. // this works because we're accessing thePlugin object directly:
  27. if(window.console) window.console.log(thePlugin.doThat);
  28.  
  29. // this works because we're calling the function through the API itself:
  30. if(window.console) window.console.log(this.doAwesome);
  31.  
  32. // this will not work for an API function (from the outside) because doThat is not returned in the API:
  33. if(window.console) window.console.log(this.doThat);
  34.  
  35.  
  36.  
  37. return thePlugin.index;
  38. }
  39.  
  40. };
  41.  
  42. if (opts.api) {
  43. var api = {
  44. getIndex: thePlugin.getIndex,
  45. doAwesome: thePlugin.doThat,
  46. opts: opts,
  47. obj: $this
  48. };
  49.  
  50. $this.data('thePlugin.api', api);
  51. };
  52.  
  53. return this.each(function() {
  54. // start the action here
  55. thePlugin.init();
  56. });
  57. };
  58.  
  59. // plugin defaults
  60. $.fn.thePlugin.defaults = {
  61. optionA: 'testing',
  62. api: true
  63. };
  64.  
  65. // public function/method
  66. $.fn.thePlugin.ver = function() { return "jquery.thePlugin version " + ver; };
  67.  
  68. })(jQuery);
Add Comment
Please, Sign In to add comment