Difference between revisions of "User:Afuna/JQuery Template"

From Dreamwidth Notes
Jump to: navigation, search
(Created page with "(function($) { $.widget("dw.someplugin", { // magic functions / conventions options: { optiontobepassedin: undefined, optionwithdefault: "default value", ...")
 
 
Line 1: Line 1:
 +
<syntaxhighlight lang="javascript">
 
(function($) {
 
(function($) {
 
$.widget("dw.someplugin", {
 
$.widget("dw.someplugin", {
Line 44: Line 45:
 
                             // any event listeners that are defined in _create
 
                             // any event listeners that are defined in _create
 
});
 
});
 +
</syntaxhighlight>

Latest revision as of 09:06, 29 March 2011

(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
});