PnP icon indicating copy to clipboard operation
PnP copied to clipboard

Multiple Taxonomy Pickers load assets inefficiently

Open SharePointRadi opened this issue 11 years ago • 6 comments

taxonomypickercontrol.js has the following:

        //load translation files
        var resourcesFile = scriptUrl + '_resources.' + this.Language.substring(0, 2).toLowerCase() + '.js';
        $.getScript(resourcesFile);

This adds a timestamp at the end of the file.

We have a custom form with 8 of those pickers and each control downloads its own resource file.

My recommendation is to remove the code that downloads the resource file outside of the control so it happens once, or figure out a way so all controls on a page coordinate a single load of the .JS file.

I am happy to do the change, however I would welcome guidance on how this should be done so I am not changing someone else's work.

(This is affecting us in our project. We also have another problem - if one of the requests takes too long it throws an exception. I will deal with that one later :) ) taxonomyresource

SharePointRadi avatar Feb 25 '15 18:02 SharePointRadi

@pbjorklund : any ideas here? You're very familiar with the taxonomypicker by now :-)

jansenbe avatar Feb 26 '15 22:02 jansenbe

A quick and dirty fix would be something like the below. We already have a global CAMControl object we can use to store the loading state.

(I thought that just allowing caching for the requests would give us "304 not modified" and be good enough, but apparently not... Anyone know why?)

Find the section i replaced by searching for the comment in the snippet below.

//load translation files
if (typeof CAMControl.resourceLoading == 'undefined') {
    CAMControl.resourceLoading = true;

    jQuery.ajax({
        dataType: "script",
        cache: true,
        url: scriptUrl + '_resources.' + this.Language.substring(0, 2).toLowerCase() + '.js'
    }).done(function () {
        delete CAMControl.resourceLoading;
    });
}

Could also just leave a resoucesLoaded behind if the page takes ages to load

//load translation files
if (typeof CAMControl.resourceLoaded == 'undefined') {
    CAMControl.resourceLoaded = false;
    var resourceFileName = scriptUrl + '_resources.' + this.Language.substring(0, 2).toLowerCase() + '.js';

    jQuery.ajax({
        dataType: "script",
        cache: true,
        url: resourceFileName
    }).done(function () {
        CAMControl.resourceLoaded = true;
    }).fail(function () {
        alert('Could not load the resource file ' + resourceFileName);
    });
}

Or we could load the resource file before setting up taxpickers and remove it from within the taxpicker constructor

var resourcesFile = scriptUrl + '_resources.' + this.Language.substring(0, 2).toLowerCase() + '.js';
$.getScript(resourcesFile, function () {
    $('#taxPickerKeywords').taxpicker({ isMulti: true, allowFillIn: true, useKeywords: true }, context);
    $('#taxPickerKeywords').taxpicker({ isMulti: true, allowFillIn: true, useKeywords: true }, context);
    $('#taxPickerKeywords').taxpicker({ isMulti: true, allowFillIn: true, useKeywords: true }, context);
});

pbjorklund avatar Feb 27 '15 15:02 pbjorklund

Thanks Patrik!

jansenbe avatar Feb 27 '15 18:02 jansenbe

I created pr #501 to handle this. Perhaps that will solve your problem @SharePointRadi ?

pbjorklund avatar Mar 04 '15 10:03 pbjorklund

Btw it seems like loading resources does not work at all (even before changes) when using ASP.NET MVC bundling.

In my current project I just stripped out all resource loading and use the bundling to do it instead

bundles.Add(new ScriptBundle("~/bundles/taxonomypicker").Include(
    "~/Scripts/taxonomypickercontrol_resources.sv.js",
    "~/Scripts/taxonomypickercontrol.js"));

pbjorklund avatar Mar 04 '15 12:03 pbjorklund

Fixed via merging of #501.

jansenbe avatar Mar 05 '15 08:03 jansenbe