Module: Ovto
- Defined in:
- lib/ovto.rb,
lib/ovto/app.rb,
lib/ovto/fetch.rb,
lib/ovto/state.rb,
lib/ovto/actions.rb,
lib/ovto/runtime.rb,
lib/ovto/version.rb,
lib/ovto/component.rb,
lib/ovto/wired_actions.rb,
lib/ovto/pure_component.rb
Defined Under Namespace
Classes: Actions, App, Component, PureComponent, Runtime, State, WiredActions
Constant Summary collapse
- VERSION =
'0.5.0'
Class Method Summary collapse
-
._do_fetch(url, init) ⇒ Object
Create an Opal Promise to call fetch API.
- .debug_trace ⇒ Object
- .debug_trace=(bool) ⇒ Object
- .debug_trace_log(msg) ⇒ Object
-
.fetch(url, method = 'GET', data = nil) ⇒ Object
Wrapper for the fetch API The server must respond a json text.
-
.inspect(obj) ⇒ Object
JS-object-safe inspect.
-
.log_error(&block) ⇒ Object
Call block.
Class Method Details
._do_fetch(url, init) ⇒ Object
Create an Opal Promise to call fetch API
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/ovto/fetch.rb', line 34 def self._do_fetch(url, init) promise = Promise.new text = error = nil %x{ fetch(url, init).then(response => { if (response.ok) { return response.text(); } else { throw response; } }).then(text => #{promise.resolve(JSON.parse(text))} ).catch(error => #{promise.reject(error)} ); } return promise end |
.debug_trace ⇒ Object
20 |
# File 'lib/ovto.rb', line 20 def self.debug_trace; @debug_trace; end |
.debug_trace=(bool) ⇒ Object
21 |
# File 'lib/ovto.rb', line 21 def self.debug_trace=(bool); @debug_trace = bool; end |
.debug_trace_log(msg) ⇒ Object
22 23 24 |
# File 'lib/ovto.rb', line 22 def self.debug_trace_log(msg) console.log("Ovto: "+msg) if @debug_trace end |
.fetch(url, method = 'GET', data = nil) ⇒ Object
Wrapper for the fetch API The server must respond a json text.
Example:
Ovto.fetch('/api/new_task', 'POST', {title: "do something"}).then{|json_data|
p json_data
}.fail{|e| # Network error, 404 Not Found, JSON parse error, etc.
p e
}
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/ovto/fetch.rb', line 15 def self.fetch(url, method='GET', data=nil) init = `{ method: #{method}, credentials: 'same-origin' // Send cookies to the server (eg. for CookieStore of Rails) }` if method != 'GET' %x{ var headers = {'Content-Type': 'application/json'}; var metaTag = document.querySelector('meta[name=csrf-token]'); if (metaTag) headers['X-CSRF-Token'] = metaTag.content; init['headers'] = headers; init['body'] = #{data.to_json}; } end return _do_fetch(url, init) end |
.inspect(obj) ⇒ Object
JS-object-safe inspect
27 28 29 30 31 32 33 |
# File 'lib/ovto.rb', line 27 def self.inspect(obj) if `obj.$inspect` obj.inspect else `JSON.stringify(#{obj}) || "undefined"` end end |
.log_error(&block) ⇒ Object
Call block. If an exception is raised and there is a tag with ‘id=’ovto-debug’‘, describe the error in that tag
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/ovto.rb', line 37 def self.log_error(&block) return block.call rescue Exception => ex raise ex if `typeof document === 'undefined'` # On unit tests div = `document.getElementById('ovto-debug')` if `div && !ex.OvtoPrinted` %x{ div.textContent = "ERROR: " + #{ex.class.name}; var ul = document.createElement('ul'); // Note: ex.backtrace may be an Array or a String #{Array(ex.backtrace)}.forEach(function(line){ var li = document.createElement('li'); li.textContent = line; ul.appendChild(li); }); div.appendChild(ul); ex.OvtoPrinted = true; } end raise ex end |