Class: Isomorfeus::TopLevel

Inherits:
Object
  • Object
show all
Defined in:
lib/isomorfeus/top_level_ssr.rb,
lib/isomorfeus/top_level_browser.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.ssr_route_pathObject

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_urlObject

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



7
8
9
# File 'lib/isomorfeus/top_level_ssr.rb', line 7

def mount!
  # nothing, but keep it for compatibility with browser
end

.mount_component(component, props, element_or_query, hydrated = false) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/isomorfeus/top_level_browser.rb', line 45

def self.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

  Isomorfeus.top_component = if hydrated
                               ReactDOM.hydrate(React.create_element(component, props), element)
                             else
                               ReactDOM.render(React.create_element(component, props), element)
                             end
end

.on_ready(&block) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/isomorfeus/top_level_browser.rb', line 26

def self.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



37
38
39
40
41
42
43
# File 'lib/isomorfeus/top_level_browser.rb', line 37

def self.on_ready_mount(component, props = nil, element_query = nil)
  # init in case it hasn't been run yet
  Isomorfeus.init
  on_ready do
    mount_component(component, props, element_query)
  end
end

.render_component_to_string(component_name, props_json) ⇒ Object



11
12
13
14
15
# File 'lib/isomorfeus/top_level_ssr.rb', line 11

def render_component_to_string(component_name, props_json)
  # for some reason props_json arrives already decoded
  component = Isomorfeus.cached_component_class(component_name)
  ReactDOMServer.render_to_string(React.create_element(component, `Opal.Hash.$new(props_json)`))
end