Class: React::IsomorphicHelpers::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/reactive-ruby/isomorphic_helpers.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(unique_id, ctx = nil, controller = nil, cname = nil) ⇒ Context

Returns a new instance of Context.



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/reactive-ruby/isomorphic_helpers.rb', line 115

def initialize(unique_id, ctx = nil, controller = nil, cname = nil)
  @unique_id = unique_id
  @cname = cname
  if RUBY_ENGINE != 'opal'
    @controller = controller
    @ctx = ctx
    if defined? @@ctx_methods
      @@ctx_methods.each do |method_name, block|
        @ctx.attach("ServerSideIsomorphicMethod.#{method_name}", proc{|args| block.call(args.to_json)})
      end
    end
  end
  Hyperloop::Application::Boot.run(context: self)
  self.class.before_first_mount_blocks.each { |block| block.call(self) }
end

Instance Attribute Details

#controllerObject (readonly)

Returns the value of attribute controller.



99
100
101
# File 'lib/reactive-ruby/isomorphic_helpers.rb', line 99

def controller
  @controller
end

#unique_idObject (readonly)

Returns the value of attribute unique_id.



100
101
102
# File 'lib/reactive-ruby/isomorphic_helpers.rb', line 100

def unique_id
  @unique_id
end

Class Method Details

.before_first_mount_blocksObject



107
108
109
# File 'lib/reactive-ruby/isomorphic_helpers.rb', line 107

def self.before_first_mount_blocks
  @before_first_mount_blocks ||= []
end

.define_isomorphic_method(method_name, &block) ⇒ Object



102
103
104
105
# File 'lib/reactive-ruby/isomorphic_helpers.rb', line 102

def self.define_isomorphic_method(method_name, &block)
  @@ctx_methods ||= {}
  @@ctx_methods[method_name] = block
end


111
112
113
# File 'lib/reactive-ruby/isomorphic_helpers.rb', line 111

def self.prerender_footer_blocks
  @prerender_footer_blocks ||= []
end

.register_before_first_mount_block(&block) ⇒ Object



150
151
152
# File 'lib/reactive-ruby/isomorphic_helpers.rb', line 150

def self.register_before_first_mount_block(&block)
  before_first_mount_blocks << block
end


154
155
156
# File 'lib/reactive-ruby/isomorphic_helpers.rb', line 154

def self.register_prerender_footer_block(&block)
  prerender_footer_blocks << block
end

Instance Method Details

#eval(js) ⇒ Object



135
136
137
# File 'lib/reactive-ruby/isomorphic_helpers.rb', line 135

def eval(js)
  @ctx.eval(js) if @ctx
end

#load_opal_contextObject



131
132
133
# File 'lib/reactive-ruby/isomorphic_helpers.rb', line 131

def load_opal_context
  send_to_opal(:load_context, @unique_id, @cname)
end

#send_to_opal(method_name, *args) ⇒ Object



139
140
141
142
143
144
145
146
147
148
# File 'lib/reactive-ruby/isomorphic_helpers.rb', line 139

def send_to_opal(method_name, *args)
  return unless @ctx
  args = [1] if args.length == 0
  ::ReactiveRuby::ComponentLoader.new(@ctx).load!
  method_args = args.collect do |arg|
    quarg = "#{arg}".tr('"', "'")
    "\"#{quarg}\""
  end.join(', ')
  @ctx.eval("Opal.React.$const_get('IsomorphicHelpers').$#{method_name}(#{method_args})")
end