Created
August 24, 2012 00:50
-
-
Save joshuap/3444131 to your computer and use it in GitHub Desktop.
Rails 3.2 route-based custom JQuery events
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(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') | |
}); | |
}); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ -> | |
$(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