views:

34

answers:

1

Hi guys,

I'm trying to adopt Jammit in my Rails application.

Default config provided in documentation grabs all js files including view specific javascript:

embed_assets: on

javascripts:
  workspace:
    - public/javascripts/vendor/jquery.js
    - public/javascripts/lib/*.js
    - public/javascripts/views/**/*.js
    - app/views/workspace/*.jst

stylesheets:
  common:
    - public/stylesheets/reset.css
    - public/stylesheets/widgets/*.css
  workspace:
    - public/stylesheets/pages/workspace.css
  empty:
    - public/stylesheets/pages/empty.css

Let's consider a case when view specific javascript should be executed only on certain view:

$(function(){
  alert("View specific message here!");
}

How can I avoid such effect?

Regards, Alexey Zakharov

+3  A: 

My preference is to wrap up that "view-specific-javascript" in a function. And then call that function depending on the page you actually load. In this way, all of your JavaScripts can be cached by browsers as a single file, and you can execute the portions of the JS that you need.

So I'd add a <script> tag to the particular html.erb template that calls your view-specific function on page load.

Hope that helps...

jashkenas
Remember in Haml you can use a `:javascript` filter to call that function too. This will write out the correct script tag and wrap it in CDADA. If you want it to go in the head section of your layout use `content_for`. :filter $(function() { callFunction() });
Andy Atkinson
Thanks Jeremy.Do you mean adding something like this: <script type="text/javascript"> Frontend.Product.Index.init() </script> Frontend.Product.Category = { init : function() { var dialogWindow = $(".product-dialog").dialog( { autoOpen : false, modal : true, draggable : false, resizable : false }); }}
Alexey Zakharov
Alexey: Precisely. You should have a single top-level entry function for each page that needs to behave in a different way. And then just call the appropriate one...
jashkenas