Class: Tilt::ReactTemplate

Inherits:
Template
  • Object
show all
Defined in:
lib/tilt/react.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file = nil, line = 1, options = {}, &block) ⇒ ReactTemplate

Returns a new instance of ReactTemplate.



9
10
11
12
# File 'lib/tilt/react.rb', line 9

def initialize(file=nil, line=1, options={}, &block)
  file = File.expand_path(file)
  @component_class = File.basename(file, '.jsx').split('_').map(&:capitalize).join
end

Class Method Details

.componentsObject



44
45
46
# File 'lib/tilt/react.rb', line 44

def self.components
  @@context.eval('window.TiltReact.componentNames()').to_a
end

.file_to_class_name(file) ⇒ Object



40
41
42
# File 'lib/tilt/react.rb', line 40

def self.file_to_class_name(file)
  File.basename(file, '.jsx').split('_').map(&:capitalize).join
end

.load_directory(glob) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/tilt/react.rb', line 26

def self.load_directory(glob)
  Dir.glob(glob).to_a.sort_by { |bundle|
    case bundle
    when %r{/tilt_react_client_bundle.js$} then 0
    when %r{/tilt_react_server_bundle.js$} then 1
    else 2
    end
  }.each do |bundle|
    @@context.load(bundle)
  end

  @@renderer = @@context.eval('window.TiltReact')
end

.prepare_contextObject



21
22
23
24
# File 'lib/tilt/react.rb', line 21

def self.prepare_context
  @@context = V8::Context.new
  @@context.eval('window = {};')
end

Instance Method Details

#evaluate(scope, props, &block) ⇒ Object



14
15
16
17
18
19
# File 'lib/tilt/react.rb', line 14

def evaluate(scope, props, &block)
  @output ||= begin
    component = @@renderer.renderToString(@component_class, props)
    %{<div data-react-class="#{@component_class}">#{component}</div><script data-react-class="#{@component_class}" type="application/json">#{props.to_json}</script>}
  end
end