Kinda deprecated - not sure anyone would use this anymore with the amazing collection of JS frameworks available.¶ ↑
The humble goal of this project was to namespace JS automatically on Rails projects based on the controller and action name.
If you are on Users#new
Instead of hunting for the JS that might be running on this page. You already know where to look:
SiteName.users.new_page();
It a convention over “hunting for selectors” pattern.
gem install js_namespace_framework (or use bundler)
-
Define
SITE_NAME
somewhere
probably:
config/initializers
-
Add
<%= init_javascript if requires_javascript? %>
to the bottom of your layout.
When you need some JS Love on a page Lets say
controller: 'messages', action: 'show'
-
simply define the following method in the messages_helper.rb
def requires_javascript? return true if action_is? 'show' end
-
This will generate the following namespaced call.
SiteName.controller.action_page();
-
In this case
SiteName.messages.show_page();
-
Then just define your namespace and method
SiteName = {} SiteName.messages = { show_page: function() { SiteName.awesomeness.activate(); } }
-
If you need some more progressive enhancement on messages.index
-
In the messages_helper.rb
def requires_javascript? return true if action_is? 'show', 'index' end
-
In a Js file
SiteName = {} SiteName.messages = { show_page: function() { SiteName.awesomeness.activate(); }, index_page: function() { SiteName.progressively.enhance(); } }
I would recommend creating one Js file per controller to keep things nice and neat and using Asset Packager to pack everything together for production.
Copyright © 2009 [Brent Greeff], released under the MIT license