JQuery UI Tabs caching

前端 未结 4 1268
不思量自难忘°
不思量自难忘° 2020-12-09 06:18

I am working in an ASP .net MVC Application. I have a JQuery UI tab whose Javascript to enable caching is as follows.

function LoadStudentDetailTabs() {
            


        
相关标签:
4条回答
  • 2020-12-09 06:49

    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);
    

    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 }
        ]
    });
    

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

    0 讨论(0)
  • 2020-12-09 06:53

    None of the above worked for me in IE. I had to put

    [OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]
    

    At the page level, rather than on the Ajax method (which worked for Chrome)

    So:

    [OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]
    public ViewResult MyPage()
    {
    }
    

    As well as:

    [OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]
    public JsonResult MethodCalledByAjax()
    {
    }
    
    0 讨论(0)
  • 2020-12-09 06:54

    So, simplifying Eric's analysis, you can control the caching of each tab by setting the 'cache.tabs' data in each tab's anchor element.

    // disable cache by default
    $("#tabs").tabs({
        cache: false,
    });
    

    Then after the tab content has been loaded for the first time, you can enable the caching for that tab. I would simply put it in the $(document).ready:

    $(document).ready(function () {
        // cache content for the current tab
        var currentTabIndex = $("#tabs").tabs('option', 'selected');
        var currentTabAnchor = $("#tabs").data('tabs').anchors[currentTabIndex];
        $(currentTabAnchor).data('cache.tabs', true)
    });
    

    Thanks, Eric!

    0 讨论(0)
  • 2020-12-09 07:06

    Try this

    $(document).ready(function(){
      $("#tabs").tabs({
        spinner: 'Loading...',
        cache: false,
        ajaxOptions: {cache: false}
      });
    });
    
    0 讨论(0)
提交回复
热议问题