I am sure that you can find this question asked by beginner PHP developers, Perl, ASP and any other server languages there. There do not seem to be too many cases where you want your javascript code to be embedded .. the only one that might be that you configure it in a specific situation, but from my point of view I can’t think of any situations, when you need to configure it that way.
Accordingly, if you have a lot of javascript, and you include all this in your output, you will inflate your page. Page 1k will turn into a page> 100k Pages if you add a large application. And along with this, it will most likely be more difficult to cache this information, since you will probably configure the output a little if the user is logged in, or if there is a special announcement that day, etc. You are much better off decoupling it from your own file so that you can "minimize" it ( http://en.wikipedia.org/wiki/Minification_%28programming%29 ).
From developer to developer, I can tell you terrible stories about how javascript (and html) is embedded in PHP and Smarty templates. Be nice to whoever you work with and do not split all the languages into your own files. Let's say you work with Bob, and he's a great guy, but knows nothing about the application and how he spills out his code. If the javascript that will look like in erb is disappointing due to the lack of a single truth point for the application - if it wants to change something, it will look through all your erb to find out exactly where it should put it or where it was originally from . And do you really want someone who knows little about your backend structure to dig into these files?
There are several cases where you want to put boot data. If you have a lot of data to enter the page at startup, then when assigning several variables at page startup, it is more efficient than exiting and executing an ajax request every time the page loads.
Please be kind to us guys, and if you have no good reason, keep JS in separate files :)
Edit: here is an example of what you want (I think), without using template javascript:
app.js:
postUpdater = function (data) { $tr = $('<tr>'); $td1 = $('<td>').text(data.title); $td2 = $('<td>').text(data.content); $tr.append($td1, $td2); $('table tbody').append($tr); }; $("#save_button").click(function () { $.post('/save/ur/', {the:data}, postUpdater); });
In ruby, you just need to render @post for json. I think this is literally @ post.to_json, but you might need require 'json' to make it work. It may lead you there faster, but: output formated json with rails 3 (I'm not really a ruby guy .. we just use him in the company I work for, of course, I picked him up)
Now, regarding the theoretical question of why not just release a javascript template file. Let's say you have a whole bunch of things that should happen when you save this javascript file. This means that you have to output a lot of things on this ajax request and execute it. You will be pushing a larger file over the Internet (a bit slower) and you may have the “eval” response (eval is evil). So this is one bad thing. Another, perhaps badly, is that you lose access to where you called this salvation. So, if I have this opinion ..
new PostViewer({ savePost: function () { var self = this; $.post('/save/url/', {the:data}, function (response) { self.trigger('saveSuccessful', response); }); } });
When you call savePost it will be more difficult (without the possibility of introducing numerous hacks) just to tell the opinion "Hey, the rescue was successful!"
Hope a little more for the purpose for what you were looking for.