Browse the Ruby on Rails Community.

You are here: Forums Ask a Rails expert how do you organize javascript...

Replytotopic

how do you organize javascript code in your rails project?

Posted in Forums : Ask a Rails expert

 
Profile

Authority 12
Posting Rating 0
Sign in to rate this post

how do you organize javascript code in your rails project?

Here is how I organize javascript code in my current project: I include common javascript files on the bottom of a layout file and yield :javascript content. It looks like this:

= include_javascripts :common
....
= yield :javascript

Then in every page rendered, I put the related javascript code at the bottom of the page, it looks like this:

-content_for :javascript :javascript //code goes here…

The question is should I put the code in to a separate javascript file or it is okay to put javascript with the html content it manipulates? Any advantages and disadvantages?

In addition, there could be some javascript code that manipulate html content in partials, how do you organize them?

In summary, what is your best practice for organizing javascript code in your rails project(especially there are a lot of javascript code)?

Thanks.

 
Profile

Authority 12
Posting Rating 0
Sign in to rate this post

Hi,

The question is should I put the code in to a separate javascript file or it is okay to put javascript with the html content it manipulates? Any advantages and >disadvantages?

you should put as much js-logic as you can into your static js-files under public/javascripts/* – so browsers can cache your static js-logic.
I use the “content_for :javascript”-technique for dynamic js-stuff. Ajax: if its dynamic js you could integrate the ajax-specific javascript
into the ajax-triggering view and/or into the resulting ajax-answer, your choice.

 
Profile

Authority 12
Posting Rating 0
Sign in to rate this post

Hey Falk,

Thanks for your suggestion. But I think javascript code can also be cached with “content_for :javascript”-technique.
For pages that need specific javascript code besides common javascript code included in layout file,
we could include them like this:

-content_for :javascript = javascript_include_tag :specific_javascript_file_for_current_page

About dynamic js code, I agree with you, we have to put the javascript code into ajax-triggering view.

 
Profile

Authority 50
Posting Rating 0
Sign in to rate this post

I think you should prevent yielded javascript code as much as possible. I always try to keep the javascript code in .js files and use describing CSS classes and semantic tags for jQuery to hook in on event and Ajax handling (I really recommend you to use jQuery instead of any other javascript library!). The only javascript code printed in your layout could be the authenticity token definition which enables you to use it in javascript.

(in application_helper.rb)
def define_authenticity_token
  javascript_tag ”$.authenticity_token = ’#{form_authenticity_token}’;”
end

(e.g. in layouts/application.html.erb)
<%= define_authenticity_token %>

As for organized javascript code in your rails application: use the Yahoo javascript module pattern! Doing this prevents your javascript files from just having dangling functions because you put them within seperate javascript modules.

Also I have created a gem called Jzip (http://github.com/archan937/jzip) which is inspirated by SASS and AssetPackager. This gem helps you to put javascript modules into seperate files as Jzip merges (and also minifies when desired) javascript files based on Jzip templates (each template stands for an outputted .js file). You can use Jzip partials which you can reuse for shared javascript files.

To keep the javascript files and Jzip templates / partials organized, I put them in namespaces and directories (just like with Ruby modules). This file is an example of a javascript module and this file ensures every init() function being called on inclusion of the javascript file. This Jzip template will be compiled to this javascript file.

For a complete example of Jzip templates, partials and javascript files, please visit http://github.com/archan937/jzip/tree/master/test/javascripts/assets/jzip/.

I use Jzip, jQuery and the javascript module pattern in every new rails project I start, it’s the only way to fly :P

I hope my answer can be of great help. Good luck!

 
Img_0146-150x150

Authority 62
Posting Rating 0
Sign in to rate this post

I agree with Paul, yielding javascript on every page is sub-optimal. You are best off putting all your javascript functions in the main application.js and reference that right before the closing </body> tag. This way the entire javascript file can be minified, cached and its functions available to you on every page.

Then when you need to change page behaviour with javascript, never write an inline piece of code, but instead put it in the main .js file, in the document ready function:

$(document).ready(function(){ // Your code here });

If you want to call a function on, for example, the contact page, use a page-specific selector in the main javascript file. For instance, to add behaviour to the contact form use $(“form#new_contact”) to select it, then add the behaviour you want. This way, all your javascript code is in one place where you can easily find it.

When the main .js file becomes too big, use modules to organize it.

Replytotopic

Other Recent Topics

Ask a Rails expert : nested application ApplicationController get called intead of children::ApplicationController

Ask a Rails expert : Best way to structure a database for a large/static dataset

Ask a Rails expert : Ruby Developer (ROR) - Scottish based (Remote working from within the UK)

Ask a Rails expert : Above Ground Pool Supplies

Ask a Rails expert : How to get url params in observer or model in Rails 3.1

Ask a Rails expert : What can persuade you to hire Junior Ruby devs with significant PHP experience?

Ask a Rails expert : What industry value does the Ruby or Rails Certification currently have?

Ask a Rails expert : Louis Vuitton Damier Azur Canvas specially sale ( www.salecheaplouisvuitton.com )

Ask a Rails expert : ·How to check errors/puts statements from ruby files which are under cronob

Ask a Rails expert : Louis Vuitton cheap Soft Sided Luggagespecial offer( www.salecheaplouisvuitton.com )

Formatting Help
  • *bold*       _italics_      
    bq. (quotes)
  • "DSC":http://www.dsc.net
  • * or # (lists)
or cancel