【问题标题】:jQuery: How to extend an extension?jQuery:如何扩展扩展?
【发布时间】:2011-08-25 04:49:47
【问题描述】:

我看到jQuery的插件可以这样扩展:

$.ui.plugin.add('draggable', 'zIndex', {
    start: function(event, ui) { //... }
})

我想知道是否可以扩展扩展名?所以,比如说,我想在可拖动插件的 zIndex 扩展中改变一些东西(不是全部),也就是说,在启动回调函数的最开始放置一些我自己的代码。我该怎么办?如何访问该功能来代理它?

【问题讨论】:

    标签: jquery jquery-plugins jquery-widgets


    【解决方案1】:

    按照这些思路:

    (function ($) {
        function proxyPlugin(plugin, callback, option, func) {
            $.each($.ui[plugin].prototype.plugins[callback], function (k, v) {
                if (v[0] == option) {
                    var fn = v[1];
    
                    v[1] = function () {
                        func.apply(this, arguments);
    
                        fn.apply(this, arguments);
                    }
                }
            });
        }
    
        proxyPlugin('draggable', 'start', 'zIndex', function (e, ui) {
            // we are in our custom function that will be triggered before the original one.
            // our custom function receives all the arguments the original one has.
            // for example, let's add a property by the name of foo to the ui object. 
            ui.foo = true;
        });
    })(jQuery);
    

    【讨论】:

    • 看起来不错,但我会添加一些验证 $.ui[plugin] 不是 null 以及其他正在访问的属性的类似内容。我认为还需要检查fn 是否是函数。
    猜你喜欢
    • 2013-05-23
    • 2011-10-13
    • 1970-01-01
    • 2012-04-04
    • 1970-01-01
    • 1970-01-01
    • 2016-08-15
    • 2013-10-27
    • 1970-01-01
    相关资源
    最近更新 更多