Class: RenderReact::Context
- Inherits:
-
Object
- Object
- RenderReact::Context
- Defined in:
- lib/render_react/context.rb
Instance Attribute Summary collapse
-
#app ⇒ Object
readonly
Returns the value of attribute app.
-
#mode ⇒ Object
readonly
Returns the value of attribute mode.
Instance Method Summary collapse
-
#initialize(javascript_source = "", mode: :client_and_server) ⇒ Context
constructor
A new instance of Context.
- #on_client(component_name, props_hash = {}) ⇒ Object
- #on_client_and_server(component_name, props_hash = {}) ⇒ Object (also: #on_server_and_client)
- #on_server(component_name, props_hash = {}) ⇒ Object
- #render_react(*args) ⇒ Object (also: #call)
Constructor Details
#initialize(javascript_source = "", mode: :client_and_server) ⇒ Context
Returns a new instance of Context.
9 10 11 12 |
# File 'lib/render_react/context.rb', line 9 def initialize(javascript_source = "", mode: :client_and_server) @app = ExecJS.compile(javascript_source) @mode = mode end |
Instance Attribute Details
#app ⇒ Object (readonly)
Returns the value of attribute app.
7 8 9 |
# File 'lib/render_react/context.rb', line 7 def app @app end |
#mode ⇒ Object (readonly)
Returns the value of attribute mode.
7 8 9 |
# File 'lib/render_react/context.rb', line 7 def mode @mode end |
Instance Method Details
#on_client(component_name, props_hash = {}) ⇒ Object
28 29 30 31 32 |
# File 'lib/render_react/context.rb', line 28 def on_client(component_name, props_hash = {}) component_uuid = SecureRandom.uuid props_json = JSON.dump(props_hash) "<div id=\"RenderReact-#{component_uuid}\"></div><script>#{client_script(component_name, props_json, component_uuid)}</script>" end |
#on_client_and_server(component_name, props_hash = {}) ⇒ Object Also known as: on_server_and_client
39 40 41 42 43 44 |
# File 'lib/render_react/context.rb', line 39 def on_client_and_server(component_name, props_hash = {}) component_uuid = SecureRandom.uuid props_json = JSON.dump(props_hash) server_rendered = app.eval(server_script(component_name, props_json)) "<div id=\"RenderReact-#{component_uuid}\">#{server_rendered}</div><script>#{client_script(component_name, props_json, component_uuid)}</script>" end |
#on_server(component_name, props_hash = {}) ⇒ Object
34 35 36 37 |
# File 'lib/render_react/context.rb', line 34 def on_server(component_name, props_hash = {}) props_json = JSON.dump(props_hash) app.eval(server_script(component_name, props_json, true)) end |
#render_react(*args) ⇒ Object Also known as: call
14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/render_react/context.rb', line 14 def render_react(*args) case mode when :client on_client(*args) when :server on_server(*args) when :client_and_server, :server_and_client on_client_and_server(*args) else raise ArgumentError, "unknown render mode" end end |