Class: ReactiveTemplate

Inherits:
Object show all
Includes:
Events
Defined in:
lib/volt/page/reactive_template.rb

Instance Method Summary collapse

Methods included from Events

#event_chain, #listeners, #on, #remove_listener, #trigger!, #trigger_by_scope!, #trigger_for_methods!

Constructor Details

#initialize(page, context, template_path) ⇒ ReactiveTemplate

Returns a new instance of ReactiveTemplate.



4
5
6
7
8
9
# File 'lib/volt/page/reactive_template.rb', line 4

def initialize(page, context, template_path)
  # puts "New Reactive Template: #{context.inspect} - #{template_path.inspect}"
  @template_path = template_path
  @target = AttributeTarget.new
  @template = TemplateRenderer.new(page, @target, context, "main", template_path)
end

Instance Method Details

#curObject

Render the template and get the current value



25
26
27
# File 'lib/volt/page/reactive_template.rb', line 25

def cur
  @target.to_html
end

#event_added(event, scope_provider, first) ⇒ Object



11
12
13
14
15
# File 'lib/volt/page/reactive_template.rb', line 11

def event_added(event, scope_provider, first)
  if first && !@template_listener
    @template_listener = @target.on('changed') { update }
  end
end

#event_removed(event, last) ⇒ Object



17
18
19
20
21
22
# File 'lib/volt/page/reactive_template.rb', line 17

def event_removed(event, last)
  if last && @template_listener
    @template_listener.remove
    @template_listener = nil
  end
end

#skip_current_queue_flushObject

TODO: improve



30
31
32
# File 'lib/volt/page/reactive_template.rb', line 30

def skip_current_queue_flush
  true
end

#updateObject



35
36
37
# File 'lib/volt/page/reactive_template.rb', line 35

def update
  trigger!('changed')
end