I'm new to web developmen开发者_JAVA百科t, so I hope this makes sense. I am using Ruby 1.8.7 and Sinatra. I set a variable in config.ru, like
$MyFile = "file.json"
Then I call an erb file, which uses that variable to open "file.json"
, get some data and display it. So far so good.
If the user activates a drop down on the page, my javascript code listens, and is supposed to re-load the page, but with $MyFile = "file2.json"
, thus the data displayed will be different. I can detect the change, and reload the page, however I don't know how to change the variable.
Is it possible to essentially call $MyFile = "file2.json"
from javascript? Of course calling a Ruby function would be fine, too.
Again, I'm not real experienced at this stuff. I've read that AJAX may be needed, but I don't know anything about AJAX.
So you can't change server-side variables using only javascript without any extra requests. If you want to change a variable without refreshing the page, you'll have to send a request using Ajax to some url with parameters for what the new value will be. You already know how to create a route in sinatra, and how to change variables obviously, so you just need to look into a javascript framework like jQuery and its ajax library to help you with making calls.
Ajax, by the way, is basically just a way for your browser to talk to the server, making requests and getting back data, without needing to refresh the page. An ajax request in a nutshell just points at a url just like when you go to a url with a browser (GET) or submit a form (POST) and gives you back the response of that url so you can use javascript to change the page.
All that being said, you should probably rethink the way you're going about your application. For instance a global variable like $MyFile only exists once, so one user changing it will change it for all users who are on your website.
In essence, think what kinds of interactions absolutely need to happen between your server and your client, what state has to be stored and where you can store it, and look at as many examples as you can find and try to copy them and change hem into what you want to do.
Good luck!
I don't really understand the question. But config.ru is the rack configuration of your application. This not the good place for business logic.
I'm not able to understand your question clearly. But, i posted the sample code, as i assume the requirements from your question.
I'm assume the javascript code listen the function "when_change_drop_down()"
<div id="content"> THIS IS YOUR FILE.JSON CONTENT LOCATION </div>
<% $MyFile = "file.json" %>
<%= text_field_tag "myfile", "#{$MyFile}" %>
<script language="javascript">
function when_change_drop_down() {
var pars = "filename=" + document.getElementByID('myfile');
new Ajax.Updater("","/controller/action?"+pars, {
asynchronous:true,
evalScripts:true,
onComplete:function(request){
document.getElementById('content').innerHTML = request.responseText;
},
onLoading:function(request){
// Your loading porcesss
}
});
}
</script>
So You can easily change the text box value also, when the drop down changed.
I hope this is help for you.
精彩评论