module JsApplicationReloader
Constants
- VERSION
Attributes
application_name[RW]
async_js_project[RW]
redirect_url[RW]
reload_required_http_status[RW]
status_header_name[RW]
token[RW]
token_header_name[RW]
Public Class Methods
configure() { |self| ... }
click to toggle source
Nice configuration is a small twist on robots.thoughtbot.com/mygem-configure-block. Thanks!
# File lib/js_application_reloader.rb, line 27 def self.configure yield(self) end
handle_reloader_token_expiration_on_client()
click to toggle source
Expiration handler (you can customize this)
# File lib/js_application_reloader.rb, line 58 def self.handle_reloader_token_expiration_on_client <<-EOF JsApplicationReloader.handleTokenExpiration = function(xhr) { var contentType = xhr.getResponseHeader("content-type") || ""; if (contentType.indexOf('html') > -1) { $('body').html(xhr.responseText); // Display the HTML returned by xhr.responseText as you see fit } if (contentType.indexOf('json') > -1) { $('body').html(xhr.responseJSON.message); // Display the HTML returned by xhr.responseJSON.message as you see fit } return false; }; EOF end
inject_script()
click to toggle source
Must be injected into ERB template using raw TODO: Option to limit fields sent to the client for lightness
# File lib/js_application_reloader.rb, line 33 def self.inject_script <<-EOF <script type="text/javascript"> JsApplicationReloader = { token: '#{JsApplicationReloader.token}', tokenHeaderName: '#{JsApplicationReloader.token_header_name}', statusHeaderName: '#{JsApplicationReloader.status_header_name}', reloadRequiredHttpStatus: '#{JsApplicationReloader.reload_required_http_status}', applicationName: '#{JsApplicationReloader.application_name}', redirectUrl: '#{JsApplicationReloader.redirect_url}' }; // -- Define Expiration check JsApplicationReloader.isTokenExpired = function(xhr) { return (xhr.status == JsApplicationReloader.reloadRequiredHttpStatus && (xhr.getResponseHeader(JsApplicationReloader.statusHeaderName) === 'token_expired')); }; #{handle_reloader_token_expiration_on_client} #{JsApplicationReloader.non_async_js_request_script unless JsApplicationReloader.async_js_project} </script> EOF end
non_async_js_request_script()
click to toggle source
For non-async Javascript projects
# File lib/js_application_reloader.rb, line 74 def self.non_async_js_request_script <<-EOF $(document).ready(function() { $(document).ajaxSend(function(event, request) { if (JsApplicationReloader && JsApplicationReloader.token) { request.setRequestHeader('#{JsApplicationReloader.token_header_name}', JsApplicationReloader.token); } }); }); EOF end