Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setting prefetch headers #1744

Closed
comply-online opened this issue Dec 26, 2018 · 1 comment
Closed

Setting prefetch headers #1744

comply-online opened this issue Dec 26, 2018 · 1 comment

Comments

@comply-online
Copy link

I followed #1254 to try to set specific headers but it does not seem to apply to prefetch.

I used the following code:

var requirements = new Bloodhound({
            datumTokenizer: Bloodhound.tokenizers.obj.whitespace('name'),
            queryTokenizer: Bloodhound.tokenizers.whitespace,
            prefetch: {
                url: apiPathRequirements,
                filter: function (list) {
                    return $.map(list, function (requirement) {
                        return { name: requirement.name };
                    });
                },
                prepare: function (query, settings) {
                    settings.headers = {
                        "Authorization": getCookie("Authorization"),
                        "comply-username": getCookie("comply-username")
                    };
                    return settings;
                }
            }
        });

I get the following error when running this code:

pages-questions.html:303 Uncaught TypeError: Cannot set property 'headers' of undefined
    at Prefetch.prepare (pages-questions.html:303)
    at Prefetch.fromNetwork (typeahead.bundle.js:595)
    at Bloodhound.loadPrefetch [as _loadPrefetch] (typeahead.bundle.js:844)
    at Bloodhound.initialize [as _initialize] (typeahead.bundle.js:859)
    at Bloodhound.initialize (typeahead.bundle.js:866)
    at new Bloodhound (typeahead.bundle.js:817)
    at pages-questions.html:292
prepare @ pages-questions.html:303
fromNetwork @ typeahead.bundle.js:595
loadPrefetch @ typeahead.bundle.js:844
initialize @ typeahead.bundle.js:859
initialize @ typeahead.bundle.js:866
Bloodhound @ typeahead.bundle.js:817
(anonymous) @ pages-questions.html:292

help?

@comply-online
Copy link
Author

Fixed it with the following code:

var requirements = new Bloodhound({
            datumTokenizer: Bloodhound.tokenizers.whitespace,
            queryTokenizer: Bloodhound.tokenizers.whitespace,
            prefetch: {
                url: apiPathRequirements,
                cache: false,
                prepare: function (settings) {
                    console.log("called prepare", settings);
                    settings.beforeSend = setRequiredHeaders;
                    return settings;
                },
                transform: function (response) {
                    console.log("Called transform:", response);
                    var modifiedResponse = $.map(response, function (requirement) {
                        return requirement.name;
                    });
                    console.log(modifiedResponse);
                    return modifiedResponse;
                }
            }
        });

        // passing in `null` for the `options` arguments will result in the default
        // options being used
        $('#prefetch .typeahead').typeahead(null, {
            name: 'requirements',
            source: requirements
        });

setRequiredHeaders() function:

function setRequiredHeaders(xhr) {
  xhr.setRequestHeader("Authorization", getCookie("Authorization"));
  xhr.setRequestHeader("comply-username", getCookie("comply-username"));
  xhr.setRequestHeader("comply-groups", getCookie("comply-groups"));
  xhr.setRequestHeader("comply-organization", getCookie("comply-organization"));
  xhr.setRequestHeader("comply-orgId", getCookie("comply-orgId"));
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant