Class: ReactiveTemplate

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

Instance Attribute Summary

Attributes included from Events

#scope

Instance Method Summary collapse

Methods included from Events

#add_event_follower, #add_event_to_chains, #add_following, #all_listeners_for, #event_chain, #event_followers, #event_followings, #listeners, #on, #remove_event_follower, #remove_event_from_chains, #remove_following, #remove_listener, #trigger!, #trigger_by_scope!

Constructor Details

#initialize(context, template_path) ⇒ ReactiveTemplate

Returns a new instance of ReactiveTemplate.



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

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

Instance Method Details

#curObject

Render the template and get the current value



25
26
27
# File 'lib/volt/templates/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/templates/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/templates/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/templates/reactive_template.rb', line 30

def skip_current_queue_flush
  true
end

#updateObject



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

def update
  trigger!('changed')
end