Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

79
Views
Avoid loading XHR data each time

I want to create a form with two fields. One is the starting station and the other is the ending station. The stations are stored in a JSON file {[],[],[]} This json file is about 400KB in size. I want to initialise the stations only once and use them for both input fields. But I can't do that. So I rewrote it so that it loads the JSON as soon as you focus on an input field. Which is pretty stupid. I would be really happy about help. This is my current structure:

app.js

// load own suggestion script
import suggestion from './suggestion';

document.getElementById('from').addEventListener('focus', function() {
    suggestion.start('from')
});

document.getElementById('to').addEventListener('focus', function() {
    suggestion.start('to')
});

suggestion.js

export default {
    stations: [],
    formSelector: '',
    url: 'http://localhost:8090/api.php',
    getData() {
        fetch(this.url)
            .then(res => res.json())
            .then(data => { 
                // call the autocomplete function and pass the stationData               
                this.autocomplete(document.getElementById(this.formSelector), data)
            })
            .catch(err => {
                console.log(err)
            });
    },
    autocomplete(inp, arr) {
        // some code ... this code fetch the input value and iterate the stations object and create a suggestionlist. I have shortened it for this question.
    },
    start(selector) {
        this.formSelector = selector;
        this.getData();
    }
}

html

<span>from</span>
<input id="from" type="text" placeholder="Station from">

<span>to</span>
<input id="to" type="text" placeholder="Station to">
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

How is the following?
Is it what you want to do?

getData() {
    if( this.stations ) {
        this.autocomplete(document.getElementById(this.formSelector), this.stations);
    } else {
        fetch(this.url)
            .then(res => res.json())
            .then(data => { 
                // call the autocomplete function and pass the stationData               
                this.stations = data;
                this.autocomplete(document.getElementById(this.formSelector), this.stations);                    
            })
            .catch(err => {
                console.log(err)
            });
    }
},
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!