0

I have an Express app with a route like this:

app.get("/", function(req, res){
  var choices = ["Batman", "Robin"];
  res.render("home", {choices: choices});
});

so that the choices variable is available to the view. In the view I have something like this, inside of a script tag:

var my_autoComplete = new autoComplete({
    selector: "#search",
    minChars: 1,
    source: function(term, suggest){
        var choices = ["Robin", "Batman"]; //I need it from the backend actually
        term = term.toLowerCase();
        var suggestions = [];
        for (i=0;i<choices.length;i++)
            if (~choices[i].toLowerCase().indexOf(term))suggestions.push(choices[i]);
              suggest(suggestions);
        }
});

(Basically it's a vanilla JavaScript input field that I've found on Internet). For the script to work I have to define an array of choices, and in the script above I'm using a local choices variable. I want that choices variable the one coming from the backend.

How can I do that? Would this be a poor designed page anyway?

acejazz
  • 789
  • 1
  • 7
  • 27

0 Answers0