简体   繁体   English

如何模拟Jquery UI API?

[英]How can I emulate the Jquery UI API?

I've written basic jQuery plugins before, but I'm struggling to get my head around something more complex. 我之前已经写了基本的jQuery插件,但是我正在努力使事情变得更复杂。 I'm looking to emulate the API of jQuery UI, which works like this: 我正在寻找模仿jQuery UI的API,其工作原理如下:

$('#mydiv').sortable({name: 'value'}); // constructor, options
$('#mydiv').sortable("serialize"); // call a method, with existing options
$('#mydiv').sortable('option', 'axis', 'x'); // get an existing option

I've tried the following: 我尝试了以下方法:

(function($){
    $.fn.myPlugin = function(cmd){
        var config = {
            default: 'defaultVal'
        };

        if(typeof cmd === 'object'){
            $.extend(config, cmd);
        }

        function _foo(){
            console.log(config.default);
        }

        if(cmd==='foo'){
            return _foo();
        }

        this.each(function(){
            // do default stuff
        });
    }
})(jQuery);

$('#myElement').myPlugin({default: 'newVal'});
$('#myElement').myPlugin('foo');

What I would like to see here is 'newval' being logged, but I'm seeing 'defaultVal' instead; 我想在这里看到的是'newval',但是我看到的是'defaultVal'。 the plugin is being called and started from scratch every time I call .myPlugin() on the element. 每次我在元素上调用.myPlugin()时,都会从头开始调用插件并从头开始。

I've also tried using _foo.call(this) and some other variants. 我也尝试使用_foo.call(this)和其他一些变体。 No joy. 不开心

In a way, I understand why this is happening, but I know that it must be possible to do it the same way as jQuery UI. 从某种意义上说,我理解为什么会发生这种情况,但是我知道一定有可能做到与jQuery UI相同的方式。 I just can't see how! 我只是看不出来!

(I appreciate that jQuery UI uses the widget factory to handle all of this, but I don't want to make that a requirement for the plugin.) (我很欣赏jQuery UI使用小部件工厂来处理所有这些,但是我不想将此作为插件的要求。)

Perhaps what you want is this... 也许您想要的是...

(function($){

    var config = {
        default: 'defaultVal'
    };

    $.fn.myPlugin = function(cmd){

        if(typeof cmd === 'object'){
            $.extend(config, cmd);
        }

        function _foo(){
            console.log(config.default);
        }

        if(cmd==='foo'){
            return _foo();
        }

        this.each(function(){
            // do default stuff
        });
    }
})(jQuery);

$('#myElement').myPlugin({default: 'newVal'});
$('#myElement').myPlugin('foo');

Move the config variable outside the myPlugin function. config变量myPlugin函数之外。 This change will cause config to be initialized only once: when your plugin function is created. 此更改将导致config仅初始化一次:创建插件函数时。

You're declaring config during the function call rather than as a closure used by it. 您在函数调用期间声明config ,而不是将其用作其闭包。 Try this: 尝试这个:

(function($){
    var config = {
        default: 'defaultVal'
    };
    $.fn.myPlugin = function(cmd){

        if(typeof cmd === 'object'){
            $.extend(config, cmd);
        }

        function _foo(){
            console.log(config.default);
        }

        if(cmd==='foo'){
            return _foo();
        }

        this.each(function(){
            // do default stuff
        });
    }
})(jQuery);

$('#myElement').myPlugin({default: 'newVal'});
$('#myElement').myPlugin('foo');

In addition, you could look into the jQuery data API for caching data, especially if you aren't going to have just one instance per page. 此外,您可以研究用于缓存数据的jQuery数据API ,特别是如果您每页不会只有一个实例时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM