Class: Isomorfeus::TopLevel
- Inherits:
-
Object
- Object
- Isomorfeus::TopLevel
- Defined in:
- lib/isomorfeus/top_level.rb,
lib/isomorfeus/top_level_ssr.rb
Class Attribute Summary collapse
-
.ssr_route_path ⇒ Object
Returns the value of attribute ssr_route_path.
-
.transport_ws_url ⇒ Object
Returns the value of attribute transport_ws_url.
Class Method Summary collapse
- .mount! ⇒ Object
- .mount_component(component, props, element_or_query, hydrated = false) ⇒ Object
- .on_ready(&block) ⇒ Object
- .on_ready_mount(component, props = nil, element_query = nil) ⇒ Object
- .render_component_to_string(component_name, props) ⇒ Object
Class Attribute Details
.ssr_route_path ⇒ Object
Returns the value of attribute ssr_route_path.
4 5 6 |
# File 'lib/isomorfeus/top_level_ssr.rb', line 4 def ssr_route_path @ssr_route_path end |
.transport_ws_url ⇒ Object
Returns the value of attribute transport_ws_url.
5 6 7 |
# File 'lib/isomorfeus/top_level_ssr.rb', line 5 def transport_ws_url @transport_ws_url end |
Class Method Details
.mount! ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/isomorfeus/top_level.rb', line 4 def mount! Isomorfeus.init Isomorfeus::TopLevel.on_ready do root_element = `document.querySelector('div[data-iso-root]')` component_name = root_element.JS.getAttribute('data-iso-root') Isomorfeus.env = root_element.JS.getAttribute('data-iso-env') component = nil begin component = component_name.constantize rescue Exception @timeout_start = Time.now unless @timeout_start if (Time.now - @timeout_start) < 10 `setTimeout(Opal.Isomorfeus.TopLevel['$mount!'], 100)` else `console.error("Unable to mount '" + #{component_name} + "'!")` end end if component props_json = root_element.JS.getAttribute('data-iso-props') props = `Opal.Hash.$new(JSON.parse(props_json))` hydrated = root_element.JS.getAttribute('data-iso-hydrated') state_json = root_element.JS.getAttribute('data-iso-state') if state_json %x{ var state = JSON.parse(state_json); var keys = Object.keys(state); for(var i=0; i < keys.length; i++) { global.Opal.Isomorfeus.store.native.dispatch({ type: keys[i].toUpperCase(), set_state: state[keys[i]] }); } } end begin Isomorfeus::TopLevel.mount_component(component, props, root_element, hydrated) rescue Exception @timeout_start = Time.now unless @timeout_start if (Time.now - @timeout_start) < 10 `setTimeout(Opal.Isomorfeus.TopLevel['$mount!'], 100)` else `console.error("Unable to mount '" + #{component_name} + "'!")` end end end end end |
.mount_component(component, props, element_or_query, hydrated = false) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/isomorfeus/top_level.rb', line 68 def mount_component(component, props, element_or_query, hydrated = false) if `(typeof element_or_query === 'string')` || (`(typeof element_or_query.$class === 'function')` && element_or_query.class == String) element = `document.body.querySelector(element_or_query)` elsif `(typeof element_or_query.$is_a === 'function')` && element_or_query.is_a?(Browser::Element) element = element_or_query.to_n else element = element_or_query end top = if hydrated ReactDOM.hydrate(React.create_element(component, props), element) else ReactDOM.render(React.create_element(component, props), element) end Isomorfeus.top_component = top if top end |
.on_ready(&block) ⇒ Object
49 50 51 52 53 54 55 56 57 58 |
# File 'lib/isomorfeus/top_level.rb', line 49 def on_ready(&block) # this looks a bit odd but works across _all_ browsers, and no event handler mess %x{ function run() { block.$call() }; function ready_fun() { /in/.test(document.readyState) ? setTimeout(ready_fun,5) : run(); } ready_fun(); } end |
.on_ready_mount(component, props = nil, element_query = nil) ⇒ Object
60 61 62 63 64 65 66 |
# File 'lib/isomorfeus/top_level.rb', line 60 def on_ready_mount(component, props = nil, element_query = nil) # init in case it hasn't been run yet Isomorfeus.init on_ready do Isomorfeus::TopLevel.mount_component(component, props, element_query) end end |
.render_component_to_string(component_name, props) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/isomorfeus/top_level_ssr.rb', line 11 def render_component_to_string(component_name, props) component = nil %x{ if (typeof component_name === 'string' || component_name instanceof String) { component = component_name.split(".").reduce(function(o, x) { return (o !== null && typeof o[x] !== "undefined" && o[x] !== null) ? o[x] : null; }, Opal.global) } else { component = component_name; } } component = Isomorfeus.cached_component_class(component_name) unless component ReactDOMServer.render_to_string(React.create_element(component, `Opal.Hash.$new(props)`)) end |