Skip to content

Commit

Permalink
accept "query" as a parameter and dynamically fetch data based on use…
Browse files Browse the repository at this point in the history
…r input from local institutions
  • Loading branch information
whomingbird committed Feb 12, 2025
1 parent 2288a69 commit 22a912a
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions app/assets/javascripts/institution-ror-typeahead.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,22 +111,21 @@ function rorSuggestionTemplate(data) {
}


function initializeLocalInstitutions(endpoint = '/institutions/typeahead.json', cache = false) {

function initializeLocalInstitutions(endpoint = '/institutions/typeahead.json', query = '', cache = false) {
const baseUrl = `${window.location.protocol}//${window.location.host}`;
const fullUrl = `${baseUrl}${endpoint}`;
const fullUrl = `${baseUrl}${endpoint}?q=${encodeURIComponent(query)}`;

console.log("Institutions URL: " + fullUrl);

// Initialize and return the Bloodhound instance
return new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('text'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
prefetch: {
remote: {
url: fullUrl,
cache: cache, // Use the provided cache option
wildcard: '%QUERY',
cache: cache,
transform: function (response) {
// Map the results array to the structure Bloodhound expects
return response.results;
return response.results; // Ensure response is structured correctly
}
}
});
Expand Down Expand Up @@ -169,7 +168,10 @@ $j(document).ready(function () {
{
name: 'institutions',
display: 'text', // Display the 'text' field in the dropdown
source: initializeLocalInstitutions(), // Local data source
source: function (query, syncResults, asyncResults) {
const bloodhound = initializeLocalInstitutions('/institutions/typeahead.json', query);
bloodhound.search(query, syncResults, asyncResults);
}, // Local data source
templates: {
header: '<div class="league-name">Institutions saved locally</div>',
suggestion: localSuggestionTemplate
Expand Down

0 comments on commit 22a912a

Please sign in to comment.