且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

JQuery UI 选项卡缓存

更新时间:2023-01-05 17:35:58

我最近也有这个用处.查看代码,它使用cache.tabs"和 $.data 来确定选项卡是否应该被缓存.所以你只需要抓取元素,并设置 $.data(a, "cache.tabs", false);

I recently had a use for this as well. Looking into the code, it uses "cache.tabs" with $.data to determine if a tab should be cached. So you just need to grab the element, and set $.data(a, "cache.tabs", false);

我创建了一个快速扩展来做到这一点,假设选项卡是静态的.可能存在无法预料的问题,当然可以改进.

I created a quick extension to do just that, assuming the tabs are static. There may be unforseen issues, and can certainly be improved.

(function($) {
    $.extend($.ui.tabs.prototype, {
        _load25624: $.ui.tabs.prototype.load,
        itemOptions: [],
        load: function(index) {
            index = this._getIndex(index);

            var o = this.options,
                a = this.anchors.eq(index)[0];

            try {
                if(o.itemOptions[index].cache === false)
                    $.data(a, "cache.tabs", false);
            }
            catch(e) { }

            return this._load25624(index);
        }
    });
})(jQuery);

如您所见,我将旧的 load 方法分配给 _load25624,只是一些随机名称,将其保留为对象的成员,然后在新的load 方法在确定是否应缓存选项卡后.用法:

As you can see I assign the old load method to _load25624, just some random name, keeping it as a member of the object, and call it in the new load method after having determined if the tab should be cached. Usage:

$("#tabs").tabs({
    cache: true,
    itemOptions: [
        { cache: false }
    ]
});

将为整个项目集打开缓存,并仅对第一个项目(索引 0)关闭缓存.

Will turn on cache for the entire set of items, and turn off cache for just the first item (index 0).