User:Afuna/JQuery Template

From Dreamwidth Notes
< User:Afuna
Revision as of 09:05, 29 March 2011 by Afuna (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

(function($) { $.widget("dw.someplugin", {

   // magic functions / conventions
   options: {
       optiontobepassedin: undefined,
       optionwithdefault: "default value",
   },
   _create: function() {
       // called only once. optional
       // use self, rather than this in any big function
       // to avoid issues with nested event listeners
       var self = this;
       self.element    // magic! element the widget was called on
           .click(function(e) {
               this.... // refers to the clicked element
           });
   },
   _init: function() {
       // may be called multiple times. optional
   },
   widget: function() {
       // return the outermost HTML element that the widget creates
       // e.g., a dropdown menu, a tooltip, etc
       // can be called from outside
       // as $("#someelement").someplugin( "widget" )
       return ...;
   },
   // custom functions
   publicfunction: function() {
       // something that can be called from outside
       // as $("#someelement").someplugin( "publicfunction" )
   },
   _privatefunction: function() {
       // something that can only be called internally
   }

}); })(jQuery);

jQuery(function($) {

   $("a").someplugin();    // create the widget on all links, which also initializes
                           // any event listeners that are defined in _create

});