I have an MVC architecture I would like to maintain. In my view, I have a button that will cause the chessboard to be updated according to some row in the ChessPosition database. Currently, I am inputting the data (chess position) into a hidden span and then accessing the span in JavaScript. Obviously, this is jank. I would also have trouble updating the span for the next position if the button is clicked again. What's a good way of passing the model into JavaScript? In my controller, I load the model into a variable, can I use that in JavaScript? If so, how?
HTML:
<span id="tactic_info" style="visibility: hidden"><%=generate_tactic(@tactic_model) %></span>
<button id="tactic_generator">Generate Tactic</button>
helper:
module AppsHelper
def generate_tactic(model)
id = Random.new.rand(1..Tactic.all.size)
t = model.find(id)
@tactic = [t.fen, t.solution]
end
end
JS:
let tactic, tg;
window.onload = function() {
tactic = document.getElementById("tactic_info").textContent;
tg = document.getElementById("tactic_generator");
tg.onclick = function c() {
console.log(tactic.split(',', 2));
};
};