Class: Ferro::Xhr
- Inherits:
-
Object
- Object
- Ferro::Xhr
- Defined in:
- opal/opal-ferro/ferro_xhr.js.rb
Overview
Create and execute AJAX requests. Based on turbolinks/http_request. Specify the url for the request and two callbacks, one for succesful completion of the request and one for a failed request.
It is safe to destroy the FerroXhr instance after one of it’s callbacks have completed. Note that there are no private methods in Opal. Methods that should be private are marked in the docs with ‘Internal method’.
This class can only send and receive JSON. For non-get requests a csrf token can be specified or left blank. When blank this class will try to find the token in the header meta data.
Constant Summary collapse
- TIMEOUT_FAILURE =
Error message for timeouts.
'Timeout'
Instance Method Summary collapse
-
#cancel ⇒ Object
Cancel a running AJAX request.
-
#createXHR ⇒ Object
Internal method to set up the AJAX request.
-
#csrf_token ⇒ Object
Internal method to get the csrf token from header meta data.
-
#destroy ⇒ Object
Internal method to clean up the AJAX request.
-
#initialize(url, callback, error_callback, options = {}) ⇒ Xhr
constructor
Start AJAX request.
-
#requestCanceled ⇒ Object
Internal callback method.
-
#requestFailed ⇒ Object
Internal callback method, will call the failure callback specified in initialize.
-
#requestLoaded ⇒ Object
Internal callback method, will call the success callback specified in initialize or the failure callback if the html return status is 300 or higher.
-
#requestTimedOut ⇒ Object
Internal callback method, will call the failure callback specified in initialize.
-
#send ⇒ Object
Internal method to start the AJAX request.
Constructor Details
#initialize(url, callback, error_callback, options = {}) ⇒ Xhr
Start AJAX request.
Valid options are:
-
:timeout (Integer) Request-timout
-
:accept (String) Header-Accept
-
:method (Symbol) Html method, default: :get
-
:csrf (String) Csrf token, if blank and method is not :get, meta tag ‘csrf-token’ will be used
-
:body (Hash) Hash of parameters to be sent
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'opal/opal-ferro/ferro_xhr.js.rb', line 43 def initialize(url, callback, error_callback, = {}) @url = url @callback = callback @error_callback = error_callback @timeout = [:timeout] || 5000 @accept = [:accept] || 'application/json' @method = ([:method] || :get).upcase @csrf = [:csrf] @body = [:body] ? [:body].to_json.to_s : nil @xhr = nil @sent = false createXHR send end |
Instance Method Details
#cancel ⇒ Object
Cancel a running AJAX request.
68 69 70 |
# File 'opal/opal-ferro/ferro_xhr.js.rb', line 68 def cancel @xhr.JS.abort if @xhr && @sent end |
#createXHR ⇒ Object
Internal method to set up the AJAX request.
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'opal/opal-ferro/ferro_xhr.js.rb', line 73 def createXHR @xhr = `new XMLHttpRequest` `#{@xhr}.open(#{@method}, #{@url}, true)` `#{@xhr}.timeout = #{@timeout}` `#{@xhr}.setRequestHeader('Accept', #{@accept})` if @method != 'GET' `#{@xhr}.setRequestHeader('X-CSRF-Token', #{csrf_token})` if @body `#{@xhr}.setRequestHeader('Content-Type', #{@accept})` `#{@xhr}.setRequestHeader('Content-Length', #{@body.length})` end end `#{@xhr}.addEventListener('load', function(){#{requestLoaded}})` `#{@xhr}.addEventListener('error', function(){#{requestFailed}})` `#{@xhr}.addEventListener('timeout', function(){#{requestTimedOut}})` `#{@xhr}.addEventListener('abort', function(){#{requestCanceled}})` nil end |
#csrf_token ⇒ Object
Internal method to get the csrf token from header meta data.
96 97 98 99 100 |
# File 'opal/opal-ferro/ferro_xhr.js.rb', line 96 def csrf_token return @csrf if @csrf token = Native(`document.getElementsByName('csrf-token')`) @csrf = token[0].content if token.length > 0 end |
#destroy ⇒ Object
Internal method to clean up the AJAX request.
103 104 105 |
# File 'opal/opal-ferro/ferro_xhr.js.rb', line 103 def destroy `#{@xhr} = null` end |
#requestCanceled ⇒ Object
Internal callback method.
141 142 143 |
# File 'opal/opal-ferro/ferro_xhr.js.rb', line 141 def requestCanceled destroy end |
#requestFailed ⇒ Object
Internal callback method, will call the failure callback specified in initialize.
126 127 128 129 130 |
# File 'opal/opal-ferro/ferro_xhr.js.rb', line 126 def requestFailed @failed = true @error_callback.call(`#{@xhr}.status`, Native(`#{@xhr}.response`)) destroy end |
#requestLoaded ⇒ Object
Internal callback method, will call the success callback specified in initialize or the failure callback if the html return status is 300 or higher.
110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'opal/opal-ferro/ferro_xhr.js.rb', line 110 def requestLoaded begin status = Native(`#{@xhr}.status`) raise if status >= 300 json = JSON.parse(`#{@xhr}.response`) @callback.call(json) rescue => error @failed = true @error_callback.call(status, error) end destroy end |
#requestTimedOut ⇒ Object
Internal callback method, will call the failure callback specified in initialize.
134 135 136 137 138 |
# File 'opal/opal-ferro/ferro_xhr.js.rb', line 134 def requestTimedOut @failed = true @error_callback.call(0, TIMEOUT_FAILURE) destroy end |
#send ⇒ Object
Internal method to start the AJAX request.
60 61 62 63 64 65 |
# File 'opal/opal-ferro/ferro_xhr.js.rb', line 60 def send if @xhr && !@sent @xhr.JS.send(@body) @sent = true end end |