Skip to content

Instantly share code, notes, and snippets.

@joshuap
Created August 24, 2012 00:50
Show Gist options
  • Save joshuap/3444131 to your computer and use it in GitHub Desktop.
Save joshuap/3444131 to your computer and use it in GitHub Desktop.
Rails 3.2 route-based custom JQuery events
$(function(){
var controllers = <%=
actions = {}
Rails.application.routes.routes.each do |route|
if route.defaults.include?(:controller)
actions[route.defaults[:controller]] ||= []
actions[route.defaults[:controller]] << route.defaults[:action] if route.defaults.include?(:action)
end
end
actions.to_json %>;
$(document).ready(function(){
$.each(controllers, function(controller, actions){
controller = controller.replace('/', '-');
if ( $('html').is('.'+controller ) )
$(document).trigger(controller+'-ready')
$.each(actions, function(i, action){
if ( $('html').is('.'+controller+'.'+action ) )
$(document).trigger(controller+'#'+action+'-ready')
});
});
});
});
$ ->
$(document).bind 'devise-sessions-ready', () ->
console.log 'yo'
$(document).bind 'devise-sessions#new-ready', () ->
console.log 'time to log in, fool.'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment