My question is about thinking through Rails app.
I have a rich ajax-application which uses google maps api, jquery, jquery ui
and a lot of jq开发者_JS百科uery plugins
such as history, browser, hotkeys and scrollto
.
Now it has a PHP backend, but I want to rewrite it in the Rails on Rails.
Would it be normal to remove all of the Rails javascripts (e.g. javascript_include_tag
) from the code and use only my?
You could do it if you felt like it. The main (only?) reason to use javascript_include_tag
is to make sure that when JS scripts hosted on your server change, your users' browsers download the new script instead of using their cached copy of the script, which they'll have if they've ever visited your site before.
Basically, if the file 'public/javascripts/myscript.js' exists, then
<%= javascript_include_tag 'myscript.js' %>
evanluates to
<script type="text/javascript" src="/javascripts/myscript.js?CHECKSUM"></script>
where CHECKSUM
is either the last modification date of the file or a checksum of its contents (I'm not sure which - suffice it to say that the value will change whenever the file changes, tricking browsers into downloading the new script).
If the javascript file in question isn't on your server (say you're pulling jQuery from Google APIs), then Rails won't know how to generate the checksum, and the result will come out looking like a regular <script>
tag (without the ?CHECKSUM
bit).
So there's no reason to use javascript_include_tag
on scripts you're not hosting yourself. But if you have your own custom script files - and especially if these will be changed with even moderate frequency - then you should probably look into using it; it's a convenient way to make sure everyone is seeing the true current version of the site.
Hope this helps!
I don't think it's normal, but you can definitely do it. Bear in mind that anything that creates JS code automatically in rails will probably break.
If you want to retain the functionality to create JS automatically, you'll need to write a javascript driver, in the same way that the jquery driver for rails work... which probably it's like reinventing the wheel, as you're already using jQuery.
You should be fine as long as you include jquery. You could also use the jquery-rails gem to easily migrate a Rails app over from the default prototype to jquery. Then, just include all your existing javascript files like usual.
精彩评论