Jquery Plugins, calling functions from other methods

前端 未结 1 1059
日久生厌
日久生厌 2021-02-06 12:57

Like so many other similar questions on here, I am writing my first jQuery plugin. It\'s intended to take a select element and replace the options with clickable list elements,

相关标签:
1条回答
  • 2021-02-06 13:28

    One way to solve this is to put your defaults, settings and bindOptions function in the methods object (or another object in the broader scope) and reference them accordingly:

    var methods = {
        defaults: {
            clickCallback: function() {}
        },
        settings: {},
    
        bindOptions: function(var1, var2, var3) {
            // Stuff to bind elements
            // Hit the click callback
            methods.settings.clickCallback.call(this);
        },
    
        // Init method
        init: function(options) {
            methods.settings = $.extend({}, methods.defaults, options);
    
            return this.each(function() {
                if (element.is('select')) {
                    $('option', element).each(function() {
                        // Stuff to create list elements
                        // Bind click handler to the new list elements
                        methods.bindOptions(var1, va2, var3);
                    });
                }
            });
        },
    
        // Disable buttons method
        disable: function(options) {
            $(elementID).children('li')
                        .removeClass('disabled')
                        .each(function() {
                methods.bindOptions(var1, var2, var3);
            });
        }
    };
    
    0 讨论(0)
提交回复
热议问题