views:

63

answers:

3

Hello, here's what I'm trying to do right.... When a user signs in and is redirect to the landing page, I want to run a bit of jquery ajax to grab and inject some content.

I originally had it in my application.js file but the problem with that is that the code is there for user's that aren't logged in, which means I have to add logic to account for that which doesn't seem clean.

What I'd like is for when the view is called to be able to include JavaScript.... I tried pasting it in the view file itself but that failed because it is loaded before JQUERY which is at the bottom of the page...

How do Rails 3 gurus handle this? Thanks!

+1  A: 

Well, you could include the jQuery file at the top.

If that's not an option, add <%= yield :javascript %> in the layout below the jQuery include, and do:

<% content_for :javascript %>
    <script type="text/javascript">
        // code goes here
    </script>
<% end %>

It will be placed in the proper location in your layout. (However, I'd recommend, for caching's sake, that you write the Javascript in an external file and include it where necessary instead of using inline Javascript. Just good practice.)

Matchu
Thats what I was thinking above but it seems very heavy. As non-signed in users have to download all that stuff... And I image as I build out an APP that every view will have situations like this... Where can I store view specific JS?
AnApprentice
I think he's already doing that. The problem is that the code won't run without jQuery, which isn't loaded at that point.
Johannes Gorset
@FRKT — Heh. Lolfail. Had added more info the answer; removed the first bit, now, since it's no longer relevant. Thanks :)
Matchu
@TheApprentice — edited to be actually relevant to your situation. Since I'm tired xD
Matchu
Thats very cool. What about having to application.js files? One for a logged in user one for a not logged in user? Possible?
AnApprentice
@TheApprentice: definitely possible. In fact, that might actually be a better solution if it meets your needs: just include the "is logged in?" login in the layout, and include the user-only script below jQuery if necessary. Probably shouldn't consider them to be `application.js` files, though: make them additions to the `application.js` file, since you may end up needing to use Javascript that applies to both those logged in and those not.
Matchu
+1  A: 

You'll make it a lot easier on yourself if you just load jQuery earlier in the document. If you load it from Google APIs, chances are it's cached in your client's browser anyway and so the otherwise negligible increase in page load time becomes none at all.

Johannes Gorset
Thanks, but lets say I would like to load jQuery last (facebook style) any options?
AnApprentice
Better yet,,, how can I add a custom JS file for a view, and append that to the end of the page, like to toss it in after the jquery load in the application layout file?
AnApprentice
@TheApprentice: I think you'd be out of luck, unless of course you rewrite your code to not require a library that isn't loaded. ;-) Besides, are you sure you should be doing this? Why do you need to load this data by JavaScript rather than render it server-side?
Johannes Gorset
@TheApprentice: You can have a view add a custom JavaScript file to the end of the layout with the `content_for` method. See the "Layouts and Rendering" guide: http://guides.rubyonrails.org/layouts_and_rendering.html
Johannes Gorset
@TheApprentice - Don't get caught in a case of premature optimization. Do you believe that the clients downloading jQuery, especially from a CDN like Google, is going to be your biggest performance issue? If doing this makes things significantly more complicated, it simply may not be worth it.
Edward M Smith
+2  A: 

Load jQuery from Google CDN and load your view specific JS after. If you are worried about Landing Page speed for non signed in users, have it Page Cached.

If you still want to optimize it further, use a reverse proxy like Sqiud. You can also use Jammit or Asset Packager(http://synthesis.sbecker.net/pages/asset_packager) to minimize your javascript and CSS.

A word of advise, you should focus your performance efforts after you finish writing your app, it's easy to get caught up in lower priority issues which will hinder your development efforts.

Greg