插件调用者中的 jQuery 方法

发布于 2024-10-22 17:56:25 字数 661 浏览 1 评论 0原文

我正在编写我的第一个 jQuery 插件,但遇到了一些障碍。基本上,这是我希望插件调用者执行的操作,但我不确定如何构建插件:

$('div.selector').myPlugin(
    'dataloader', {
    { 
        source : 'source.json',
        autoUpdate : true
    },

    buildComplete : function() {
        //Do something when the building is completed
    }
});

因此,基本上,“dataloader”值是必需的,大括号内的选项是可选设置,并且buildComplete 是一个在某件事完成后执行的函数。

我不确定如何在插件调用者中实现“buildComplete”函数(或类似函数)。我希望避免使用下面所示的方式,因为“buildComplete”对于使用的每个页面都会有所不同:

//No can do!
$('div.selector').myPlugin('dataloader').buildComplete('do something');

是否有一个基本示例,我可以在其中找到相关内容?

谢谢, 斯普里诺724

I am writing my first jQuery plugin and I ran into a few speed bumps. Here is what I would like my plugin caller to do, basically, but I'm not sure how to structure the plugin:

$('div.selector').myPlugin(
    'dataloader', {
    { 
        source : 'source.json',
        autoUpdate : true
    },

    buildComplete : function() {
        //Do something when the building is completed
    }
});

So, basically, the "dataloader" value is required, the options inside of the curly braces are optional settings, and the buildComplete is a function which is executed once something has been completed.

I'm not sure how to implement a "buildComplete" function (or the like) into the plugin caller. I would like to avoid the way shown below, since "buildComplete" would be different for each page it is used:

//No can do!
$('div.selector').myPlugin('dataloader').buildComplete('do something');

Is there a basic example where I could find something on this?

Thanks,
spryno724

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

瞎闹 2024-10-29 17:56:25

这应该可以帮助你开始

(function($){ // avoid variable polution by wrapping like this
    $.fn.myPlugin = function(dataloader,options_in){
            options = $.extend({},{ // create option object by extending empty array with default values and then overwriting with passed options
                buildComplete: $.noop // default value for buildComplete is empty function
            },options_in)

            if(condition == met){ // in certain circumstance
                options.buildComplete() // run the buildComplete function
            }

        }
})(jQuery)

// call like this
$('div.delector').myPlugin('dataloader',{ buildComplete: function(){ alert('the build is complete') }, otherOption: 'whatever' })

This should get you started

(function($){ // avoid variable polution by wrapping like this
    $.fn.myPlugin = function(dataloader,options_in){
            options = $.extend({},{ // create option object by extending empty array with default values and then overwriting with passed options
                buildComplete: $.noop // default value for buildComplete is empty function
            },options_in)

            if(condition == met){ // in certain circumstance
                options.buildComplete() // run the buildComplete function
            }

        }
})(jQuery)

// call like this
$('div.delector').myPlugin('dataloader',{ buildComplete: function(){ alert('the build is complete') }, otherOption: 'whatever' })
舞袖。长 2024-10-29 17:56:25
$.extend({
  myPlugin : function( parameters, callbackFn ){

   ...do what needs to be done...

    if( $.isFunction( callbackFn ) ){
      callbackFn.call( someArguments);
    }

  }

});

$.myPlugin( { param1: 'sdfsdf' }, function(args){
 ..here's your callback...
});
$.extend({
  myPlugin : function( parameters, callbackFn ){

   ...do what needs to be done...

    if( $.isFunction( callbackFn ) ){
      callbackFn.call( someArguments);
    }

  }

});

$.myPlugin( { param1: 'sdfsdf' }, function(args){
 ..here's your callback...
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文