I'm writing a simple web application using Nodejs, Express, and Jade. I've set up my routes appropriately. I'm having a problem with navigating between views. I am able to change views by entering the appropriate url in my browser. How can I get my application to change views on button click? I realize this may stem from a fundamental misunderstanding of the technologies I'm using.
Currently, I am trying to hit my controller via jquery ajax
$('#submit').click(function(){
$.ajax({
url: "/go",
data: "site=" + $('#website').val() + "&img=" + $('#image').val()
});
});
This successfully hits my controller:
app.get('/go', function(req, res){
res.render('view', {title: 'create', site: req.query['site'] });
This returns the proper html, however it does not get rendered as a new view.
This is going to seriously depend on how your client code is set up (e.g. are you using a framework like Backbone?). The simplest way would simply be setting click handlers for your navigation buttons that simply change window.location.href
to the URL associated with the corresponding view. Of course, this will cause a complete page reload on each view change, which may or may not be desirable, but without knowing more about the structure of your application, nobody can say anything more.
Alternatively, you can use good old links for navigation and use CSS to make them look like buttons.
Note that these considerations are independent of what server-side technology you're using.