This project is archived and is in readonly mode.

#6119 ✓stale
matt (at ifieldtech)

Patch to add the javascript_include_view_js helper method.

Reported by matt (at ifieldtech) | December 6th, 2010 @ 01:32 AM

With the release of Rails 3 and the encouragement to use unobtrusive JavaScript, I noticed that there was something missing from Rails 3; a MVC style helper (I know, bad example) for including JavaScript sources. Currently, you have to use javascript_include_tag to include your unobtrusive JavaScript sources. However, what if only half of your controller views actually use the JavaScript source? It seems sloppy to include a large JavaScript source file when it will not be used. It also seems sloppy to include unobtrusive JavaScript in a tag within your HTML view.

I expected Rails 3 to have a feature to automatically include JavaScript sources based off the current view's controller. However, that is not the case.

So, to make unobtrusive JavaScript flow better with Rails 3, I created the javascript_include_view_js helper method. What does this method do? Well, this method (when added to your layout view) will automatically load any JavaScript sources named after the current controller. If no such JavaScript source exists, then it doesn't even create the tag. For example, lets say I have a controller called "privacy" and have some awesome unobtrusive JavaScript (that will not be use anywhere else), all I have to do is place "privacy_java.js" in public/javascripts. Assuming I placed javascript_include_view_js in my layout view, the JavaScript will be automatically referenced in the appropriate script tag.

The javascript_include_view_js helper method can also handle deep (nested, treed, whatever you want to call it) controller names. For example, I have a Customer Management System (CMS) within one of my Rails applications. The Access controller handles all login and logout procedures. The controller name is "cms/access." If I need to include any JavaScript sources, I just name it the file "cms_access_java.js" and place it in public/javascripts. javascript_include_view_js will automatically format the controller so that it can hunt for the JavaScript source file.

Please take a look and tell me what you think. Is this a good contribution, or is this helper method better as a plugin?

My rails fork can be found at: github.com/mdoza/rails under the controller_java branch.

Thanks,

Matt

Comments and changes to this ticket

  • matt (at ifieldtech)

    matt (at ifieldtech) December 6th, 2010 @ 01:35 AM

    • Title changed from “The javascript_include_view_js helper method.” to “Patch to add the javascript_include_view_js helper method.”
  • Neeraj Singh

    Neeraj Singh December 6th, 2010 @ 05:09 AM

    • Importance changed from “” to “Low”

    I had developed a plugin around 6 months ago which provided exactly the same functionality you are talking about. It did not last long. Over the time we found it was more of a hassle to get the js files totally decoupled.

    It also meant one more js file for browser to get: one for base file and one for controller (if any).

    Just my thought. Of course your mileage will vary if you have a really client rich heavy project.

    I personally think something like this should start as a gem.

  • rails

    rails March 14th, 2011 @ 12:00 AM

    • Tag changed from patch rails 3.1.0.beta, javascript to patch rails 310beta, javascript
    • State changed from “new” to “open”

    This issue has been automatically marked as stale because it has not been commented on for at least three months.

    The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.

    Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.

  • rails

    rails March 14th, 2011 @ 12:00 AM

    • State changed from “open” to “stale”

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Attachments

Pages