I am trying to implement a progress bar (Javascript) with Ruby on Rails application, but I am having trouble passing values from controller to JS to update the bar.
Initially, I tried using a simple global variable ($count) but once loaded, the variable is always the same. It's not updated in the side of JS.
EDIT:
Now I am using gon gem to pass the value. But I only get the first value (that was setted when controller starts). When I call a method triggered by a button click, the value is updated but gon variable is always the same. Maybe is an error in rails/controller?
I saw an example with a method to expose variable to gon but is not working.
Note: If I remove the initialization before the search if, when search is clicked JS side shows "gon variable undefined"
Example Code: application_controller.rb
def application
@count =0 #gon assumes only this value
exposeUnProcessedJobToJavascript
if params[:search]
while(@count < 10)
exposeUnProcessedJobToJavascript
sleep 3
@count +=1
end
end
def exposeUnProcessedJobToJavascript
gon.watch.job = @count
end
end
application.js.erb
function addJobWatchers() {
if (gon.job != undefined) {
gon.watch('job', {
interval: 1000
}, updateProcessingJob);
}
}
function updateProcessingJob(job) {
if (job) {
console.log("JOB: " + job);
}
}
$(document).on('page:change', function() {
$("#search_button").click(function() {
console.log("click search");
if (typeof gon !== 'undefined') {
addJobWatchers();
}
});});