Class: JsRender::Renderer
- Inherits:
-
Object
- Object
- JsRender::Renderer
- Defined in:
- lib/js_render/renderer.rb
Constant Summary collapse
- GLOBAL_CONTEXT =
<<-JS var global = global || this; var self = self || this; var window = window || this; JS
Instance Attribute Summary collapse
-
#component_name ⇒ Object
Returns the value of attribute component_name.
-
#json_data ⇒ Object
Returns the value of attribute json_data.
-
#uuid ⇒ Object
Returns the value of attribute uuid.
Instance Method Summary collapse
- #generate_client_script ⇒ Object
- #generate_html ⇒ Object
-
#initialize(component_name, data) ⇒ Renderer
constructor
A new instance of Renderer.
- #render_component ⇒ Object
Constructor Details
#initialize(component_name, data) ⇒ Renderer
Returns a new instance of Renderer.
13 14 15 16 17 18 |
# File 'lib/js_render/renderer.rb', line 13 def initialize(component_name, data) @component_name = component_name data = data.to_json if !data.is_a?(String) @json_data = data @uuid = SecureRandom.uuid end |
Instance Attribute Details
#component_name ⇒ Object
Returns the value of attribute component_name.
5 6 7 |
# File 'lib/js_render/renderer.rb', line 5 def component_name @component_name end |
#json_data ⇒ Object
Returns the value of attribute json_data.
5 6 7 |
# File 'lib/js_render/renderer.rb', line 5 def json_data @json_data end |
#uuid ⇒ Object
Returns the value of attribute uuid.
5 6 7 |
# File 'lib/js_render/renderer.rb', line 5 def uuid @uuid end |
Instance Method Details
#generate_client_script ⇒ Object
42 43 44 45 46 47 48 49 |
# File 'lib/js_render/renderer.rb', line 42 def generate_client_script func_name = JsRender.config.client_render_function.gsub('*', @component_name) <<-HTML <script> typeof #{func_name} === 'function' && #{func_name}('#{@uuid}', #{@json_data}); </script> HTML end |
#generate_html ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/js_render/renderer.rb', line 27 def generate_html func_name = JsRender.config.server_render_function.gsub('*', @component_name) server_code = <<-JS (function () { var serverStr = typeof #{func_name} === 'function' ? #{func_name}(#{@json_data}) : ''; return '<span id="#{@uuid}">' + serverStr + '</span>'; })() JS renderer_code = js_context(find_renderer) context = ::ExecJS.compile(GLOBAL_CONTEXT + renderer_code) context.eval(server_code) rescue ExecJS::RuntimeError, ExecJS::ProgramError => error raise JsRender::ServerRenderError::new(@component_name, @json_data, error) end |
#render_component ⇒ Object
20 21 22 23 24 25 |
# File 'lib/js_render/renderer.rb', line 20 def render_component server_html = generate_html client_script = generate_client_script component = (server_html + client_script) component.respond_to?(:html_safe) ? component.html_safe : component end |