views:

59

answers:

1

We are currently in the process of refactoring our site and we have decided to just go with one js library (jquery). Problem is, due to time constraint, we can only do so much so at the moment, we have to make them coexist.

This is easy since we have jquery's no conflict method, so I can just declare jquery then do the no conflict thing and then add prototype like so:

<%= javascript_include_tag "jquery" %>

<%= yield :jquery %>

<%= javascript_include_tag "prototype", "effects", "dragdrop", "controls", "lowpro", "neo", "filter", "slider", "calendar", "application", 'alerts'  %>

I then just call content_for(:jquery) whenever I need to add a specific jquery js file(this has the no conflict function) on the partial that needs it. So no problem there.

Problem is when you include 2 partials with different jquery files. Only one of them gets called since a content_for jquery is already called, it won't be called anymore(so lets say in a.rhtml, i added a.js and in b.rhtml i added b.js, only a.js will be included)

One solution would be to just include everything in one master js file but that would be difficult to maintain since every function is there etc. Is there a way so I can just append to content_for when it already has been called?

I just checked the api:

http://apidock.com/rails/v2.3.8/ActionView/Helpers/CaptureHelper/content_for

it seems content_for actually APPENDS. But what happens to mine is it just doesn't append/clear out. It just completely ignores the second content_for call

+1  A: 

Check out Jammit. You just define your various sets of files in assets.yml and then Jammit will package them up and generate minified and gzipped versions of those files for deployment. It's very easy to use, and you can keep functionality in multiple files and then just let Jammit package it up. This improves your client render speed, too, as it reduces HTTP connection overhead, and that single master file is easily cached, resulting in a snappy user experience.

Chris Heald
Hmm, we already have bundle-fu for that(which currently isn't working) but the problem is I have to include my jquery files BEFORE calling prototype(so i can return the $ variable to it), hence the yield
corroded
You could also use `var $j = jQuery.noConflict()` and then you can use `$j` for jquery, and prototype keeps working with `$`. That way you can use them side by side.
nathanvda
My point is that if you use Jammit's assets, you can just package up all your Javascript assets in a fixed order and stop worrying about 'em. :)
Chris Heald