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 = nil, 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 = nil, mode: :client_and_server) ⇒ Context
Returns a new instance of Context.
9 10 11 12 13 14 15 16 17 |
# File 'lib/render_react/context.rb', line 9 def initialize(javascript_source = nil, mode: :client_and_server) if javascript_source == nil @app = nil @mode = :client_only else @app = ExecJS.compile(javascript_source) @mode = mode end 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
33 34 35 36 37 |
# File 'lib/render_react/context.rb', line 33 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
48 49 50 51 52 53 54 55 56 57 |
# File 'lib/render_react/context.rb', line 48 def on_client_and_server(component_name, props_hash = {}) if mode == :client_only raise ArgumentError, "Context mode is :client_only, create a server context to be able to use server-side rendering" end 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
39 40 41 42 43 44 45 46 |
# File 'lib/render_react/context.rb', line 39 def on_server(component_name, props_hash = {}) if mode == :client_only raise ArgumentError, "Context mode is :client_only, create a server context to be able to use server-side rendering" end props_json = JSON.dump(props_hash) app.eval(server_script(component_name, props_json, true)) end |
#render_react(*args) ⇒ Object Also known as: call
19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/render_react/context.rb', line 19 def render_react(*args) case mode when :client, :client_only 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 |