class React::Router::Renderer

Public Class Methods

react_props(args={}) click to toggle source
# File lib/react/router/renderer.rb, line 32
def self.react_props(args={})
  if args.is_a? String
    args
  else
    args.to_json
  end
end
render(routes, location, args={}) click to toggle source
# File lib/react/router/renderer.rb, line 26
def self.render(routes, location, args={})
  @@pool.with do |renderer|
    renderer.render(routes, location, args)
  end
end
setup!(react_js, react_server_js, react_router_js, routes_js, args={}) click to toggle source
# File lib/react/router/renderer.rb, line 15
def self.setup!(react_js, react_server_js, react_router_js, routes_js, args={})
  @@react_js = react_js
  @@react_server_js = react_server_js
  @@react_router_js = react_router_js
  @@routes_js = routes_js
  @@pool.shutdown {} if @@pool
  reset_combined_js!
  default_pool_options = {:size => 10, :timeout => 20}
  @@pool = ConnectionPool.new(default_pool_options.merge(args)) { self.new }
end

Private Class Methods

combined_js() click to toggle source
# File lib/react/router/renderer.rb, line 92
def self.combined_js
  @@combined_js
end
reset_combined_js!() click to toggle source
# File lib/react/router/renderer.rb, line 88
def self.reset_combined_js!
  @@combined_js = setup_combined_js
end
setup_combined_js() click to toggle source
# File lib/react/router/renderer.rb, line 62
      def self.setup_combined_js
        <<-CODE
          var global = global || this;
          var self = self || this;
          var window = window || this;

          var console = global.console || {};
          ['error', 'log', 'info', 'warn'].forEach(function (fn) {
            if (!(fn in console)) {
              console[fn] = function () {};
            }
          });
          function setTimeout(callback, args) {
            return callback.call();
          }

          #{@@react_js.call};
          React = global.React;
          #{@@react_server_js.call};
          ReactDOMServer = global.ReactDOMServer;
          #{@@react_router_js.call}
          ReactRouter = global.ReactRouter;
          #{@@routes_js.call};
        CODE
      end

Public Instance Methods

context() click to toggle source
# File lib/react/router/renderer.rb, line 40
def context
  @context ||= ExecJS.compile(self.class.combined_js)
end
render(routes, location, args={}) click to toggle source
# File lib/react/router/renderer.rb, line 44
      def render(routes, location, args={})
        react_props = React::Router::Renderer.react_props(args)
        jscode = <<-JS
          function() {
            var str = '';
            ReactRouter.run(#{routes}, #{location.to_json}, function (Handler) {
              str = ReactDOMServer.renderToString(React.createElement(Handler, #{react_props}));
            });
            return str;
          }()
        JS
        context.eval(jscode).html_safe
      rescue ExecJS::ProgramError => e
        raise PrerenderError.new(routes, location, react_props, e)
      end