documentcloud / jammit

Industrial Strength Asset Packaging for Rails
http://documentcloud.github.com/jammit/
MIT License
1.16k stars 197 forks source link

How do I include a dynamically-generated Javascript in my asset package? #65

Closed jamesarosen closed 14 years ago

jamesarosen commented 14 years ago

I have a view that renders some application settings into Javascript:

// in app/views/assets/settings.js
if (typeof(MyApp) === 'undefined') { MyApp = {}; }
MyApp.assetHost = '<%= ActionController::Base.asset_host %>';
// other settings that only change per reboot, not per session or request

Is there a way to tell Jammit to include the result of rendering this view in an asset package? If necessary, I could create a Rake task that would curl http://localhost/assets/settings.js > public/javascripts/generated/settings.js and use the latter in the asset package, but I can't think of a way to get Jammit to run the Rake task.

jashkenas commented 14 years ago

Any JavaScript variables that are pulled from the Rails environment, and aren't actually static, should probably be placed in your "bootstrap" JS, along with other per-session JS variables, like account_id, etc.

Do you not use a bootstrap JavaScript in conjunction with your (truly) static assets? I tend to have one as an inline script at the top of the HTML page, generated by a .erb.js partial...

agibralter commented 14 years ago

Yeah... why do this in a separation action? Seems like you could even just have a erb/haml partial that you include in your layout file. On the other hand, if you have per-user variables and you want to cache your layout, you may want to do what jashkenas does.

jamesarosen commented 14 years ago

I do have per-user and per-account JS variables. I guess I could just put the per-application stuff in there. It's not like it's a huge amount of extra bandwidth.

jashkenas commented 14 years ago

Great. That way your core JavaScripts can remain in the browser cache, even through server restarts, and asset_host changes.

jamesarosen commented 14 years ago

Jeremy: at first I read your last note as very sarcastic (users won't see the benefits of deploys). Now I realize you meant it genuinely: libraries shouldn't change across deploys, but my application code and settings certainly will.

jashkenas commented 14 years ago

Oops -- inflection is hard to communicate through github issues. No sarcasm intended...