创建具有多种功能的 jquery 插件 [英] creating a jquery plugin with multiple functions

查看:14
本文介绍了创建具有多种功能的 jquery 插件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我在我的项目中使用过很多插件,我还写了一些基本插件,这些插件只适用于带有选项的元素:

ok am kinda new to plugins i have used many in my projects, i have also written basic plugins that just work on elements with options:

(function($){
    $.fn.pulse = function(options) {
        // Merge passed options with defaults
        var opts = jQuery.extend({}, jQuery.fn.pulse.defaults, options);
        return this.each(function() {
            obj = $(this);             
            for(var i = 0;i<opts.pulses;i++) {
                obj.fadeTo(opts.speed,opts.fadeLow).fadeTo(opts.speed,opts.fadeHigh);
            };
            // Reset to normal
            obj.fadeTo(opts.speed,1);
        });
    };
    // Pulse plugin default options
    jQuery.fn.pulse.defaults = {
        speed: "slow",
        pulses: 2,
        fadeLow: 0.2,
        fadeHigh: 1
    };
})(jQuery); 

上述工作正常,但显然它执行一项任务,理想情况下我希望能够在一个插件中执行多项任务,以便我可以使用:

the above works ok, but obviously it performs one task, ideally i would like to be able to perform multiple tasks within a plugin so i could use:

$('#div').myplugin.doThis(options);
$('#div').myplugin.doThat(options;

原因是我有一个相当大的脚本,它执行各种 ajax 调用来保存数据并从数据库查询数据(使用外部 php 文件)我想将所有这些功能集成到一个插件中,但我不知道使用它的最佳结构,我看了这么多教程,基本上已经把我的脑子烧坏了,我很困惑我应该如何去做.

reason being i have quite a large script which does various ajax calls to save data and query data from a database (using an external php file) I'd like to intergrate all this functionality into a plugin, but i don't know the best structure to use for it, ive looked at so many tutorials and have basically fried my brain, and i am confused as to how i should go about doing this.

是否只是创建一个新函数的问题,例如:

is it just a question of creating a new function like:

$.fn.pluginname.dothis = function(options){
        return this.each(function() {
            //execute code
        };
   };

任何关于此的指针,或让我开始的模板都会非常有帮助.

any pointers on this, or a template to get me started would be really helpful.

永远需要帮助!!!

下一个问题:

(function($){
// Can use $ without fear of conflicts
    //var gmap3plugin = $.fn.gmap3plugin;
    var obj = this; // "this" is the jQuery object

    var methods = {
        init : function(options){
            var lat = $.fn.gmap3plugin.defaults[lat];
            var lng = $.fn.gmap3plugin.defaults[lng];
            alert('init'+' lat:'+lat+' --- lng:'+lng);
        },
        show : function( ) {  },
        hide : function( ) { },
        update : function( content ) { }
    };



    $.fn.gmap3plugin = function(method){
        // Method calling logic
        if ( methods[method] ) {
        return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
        } else if ( typeof method === 'object' || ! method ) {
        return methods.init.apply( this, arguments );
        } else {
            $.error( 'Method ' +  method + ' does not exist on jQuery.tooltip' );
        };
    };
    $.fn.gmap3plugin.defaults = {
        mapdiv :'#mapdiv',
        region : 'uk',
        lat : 53.4807125,
        lng : -2.2343765
    };
})(jQuery);

这正在运行并获得正确的传递函数,但我如何访问 $.fn.gmap3plugin.defaults 中的值我的 init 方法中的代码为 lat 和 lng 返回 undefined

this is functioning and gets the right function that is passed, but how do i access the values in the $.fn.gmap3plugin.defaults the code in my init method returns undefined for lat and lng

init : function(options){
    var lat = $.fn.gmap3plugin.defaults[lat];
    var lng = $.fn.gmap3plugin.defaults[lng];
    alert('init'+' lat:'+lat+' --- lng:'+lng);
},

或者我不能从函数访问 $.fn.gmap3plugin.defaults 中的数据吗???

or can i not access thhe data in $.fn.gmap3plugin.defaults from a function???

推荐答案

如果你看看其他一些 jQuery 插件和 jQuery UI 的设计,他们所做的就是只有一个函数 $('#div').myplugin({options}),然后他们可以通过传递字符串而不是对象来执行不同的功能$('#div').myplugin('performdifferenttask') 这又可以调用对用户隐藏的辅助函数.

If you look at the design of some of the other jQuery plugins and jQuery UI, what they do is they have a single function $('#div').myplugin({options}), and then they can do different functions by passing a string instead of an object $('#div').myplugin('performdifferenttask') which can in turn call a helper function that is hidden from the user.

例如查看 http://jqueryui.com/demos/progressbar/#methods

这里有一个例子,希望能减轻你的困惑:

Here is an example that will hopefully alleviate your confusion:

(function($) {
    $.fn.myplugin = function(options) {
        if(options == 'function1')
            function1();
        else if(options == 'function2')
            function2();
        else {
            //do default action
        }
    }

    function function1() {
        //do something
    }

    function function2() {
        //do something else
    }
}

然后在使用中:

$.myplugin({option1: 4, option2: 6}); //does default behavior
$.myplugin('function1'); //calls function1()
$.myplugin('function2'); //calls function2()

这篇关于创建具有多种功能的 jquery 插件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆