Class: JsRender::Renderer
- Inherits:
-
Object
- Object
- JsRender::Renderer
- Defined in:
- lib/js_render/renderer.rb
Constant Summary collapse
- GLOBAL_CONTEXT =
" var global = global || this;\n var self = self || this;\n var window = window || this;\n"- @@cache =
LruRedux::TTL::ThreadSafeCache.new(JsRender.config.cache_size, JsRender.config.cache_ttl)
Instance Attribute Summary collapse
-
#component_name ⇒ Object
readonly
Returns the value of attribute component_name.
-
#json_data ⇒ Object
readonly
Returns the value of attribute json_data.
-
#uuid ⇒ Object
readonly
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.
17 18 19 20 21 22 23 24 25 |
# File 'lib/js_render/renderer.rb', line 17 def initialize(component_name, data) @component_name = component_name unless data.is_a?(String) transform_keys(data) data = data.to_json end @json_data = data @uuid = SecureRandom.uuid end |
Instance Attribute Details
#component_name ⇒ Object (readonly)
Returns the value of attribute component_name.
7 8 9 |
# File 'lib/js_render/renderer.rb', line 7 def component_name @component_name end |
#json_data ⇒ Object (readonly)
Returns the value of attribute json_data.
7 8 9 |
# File 'lib/js_render/renderer.rb', line 7 def json_data @json_data end |
#uuid ⇒ Object (readonly)
Returns the value of attribute uuid.
7 8 9 |
# File 'lib/js_render/renderer.rb', line 7 def uuid @uuid end |
Instance Method Details
#generate_client_script ⇒ Object
49 50 51 52 53 54 55 56 |
# File 'lib/js_render/renderer.rb', line 49 def generate_client_script func_name = JsRender.config.client_render_function.gsub('*', @component_name) " <script>\n typeof \#{func_name} === 'function' && \#{func_name}('\#{@uuid}', \#{@json_data});\n </script>\n HTML\nend\n" |
#generate_html ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/js_render/renderer.rb', line 34 def generate_html return "<span id=\"#{@uuid}\"></span>" unless JsRender.config.should_server_render func_name = JsRender.config.server_render_function.gsub('*', @component_name) server_code = " (function () {\n var serverStr = typeof \#{func_name} === 'function' ? \#{func_name}(\#{@json_data}) : '';\n return '<span id=\"\#{@uuid}\">' + serverStr + '</span>';\n })()\n JS\n context.eval(server_code)\nrescue ExecJS::RuntimeError, ExecJS::ProgramError => error\n raise Errors::ServerRenderError::new(@component_name, @json_data, error)\nend\n" |
#render_component ⇒ Object
27 28 29 30 31 32 |
# File 'lib/js_render/renderer.rb', line 27 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 |