Class: IfBinding
Instance Attribute Summary
Attributes inherited from BaseBinding
#binding_name, #context, #target
Instance Method Summary
collapse
Methods inherited from BaseBinding
#dom_section, #remove_anchors
Constructor Details
#initialize(page, target, context, binding_name, branches) ⇒ IfBinding
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
# File 'lib/volt/page/bindings/if_binding.rb', line 4
def initialize(page, target, context, binding_name, branches)
super(page, target, context, binding_name)
getter, template_name = branches[0]
@branches = []
@listeners = []
branches.each do |branch|
getter, template_name = branch
if getter.present?
value = getter
else
value = true
end
@branches << [value, template_name]
end
@computation = -> { update }.watch!
end
|
Instance Method Details
69
70
71
72
73
74
75
76
|
# File 'lib/volt/page/bindings/if_binding.rb', line 69
def remove
@computation.stop if @computation
@computation = nil
@template.remove if @template
super
end
|
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
# File 'lib/volt/page/bindings/if_binding.rb', line 29
def update
true_template = nil
@branches.each do |branch|
value, template_name = branch
if value.is_a?(Proc)
begin
current_value = @context.instance_eval(&value)
rescue => e
Volt.logger.error("IfBinding:#{object_id} error: #{e.inspect}\n" + `value.toString()`)
current_value = false
end
else
current_value = value
end
if current_value && !current_value.nil? && !current_value.is_a?(Exception)
true_template = template_name
break
end
end
if @last_true_template != true_template
@last_true_template = true_template
if @template
@template.remove
@template = nil
end
if true_template
@template = TemplateRenderer.new(@page, @target, @context, binding_name, true_template)
end
end
end
|