Class: AttributeBinding
Instance Attribute Summary
Attributes inherited from BaseBinding
#binding_name, #context, #target
Instance Method Summary
collapse
Methods inherited from BaseBinding
#dom_section, #queue_update, #value_from_getter
Constructor Details
#initialize(page, target, context, binding_name, attribute_name, getter) ⇒ AttributeBinding
Returns a new instance of AttributeBinding.
5
6
7
8
9
10
11
12
|
# File 'lib/volt/page/bindings/attribute_binding.rb', line 5
def initialize(page, target, context, binding_name, attribute_name, getter)
super(page, target, context, binding_name)
@attribute_name = attribute_name
@getter = getter
setup
end
|
Instance Method Details
#changed(event = nil) ⇒ Object
35
36
37
38
39
40
41
42
43
44
|
# File 'lib/volt/page/bindings/attribute_binding.rb', line 35
def changed(event=nil)
case @attribute_name
when 'value'
current_value = element.value
else
current_value = element.is(':checked')
end
@value.cur = current_value
end
|
46
47
48
|
# File 'lib/volt/page/bindings/attribute_binding.rb', line 46
def element
Element.find('#' + binding_name)
end
|
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
# File 'lib/volt/page/bindings/attribute_binding.rb', line 89
def remove
case @attribute_name
when 'value'
element.off('input.attrbind', nil)
when 'checked'
element.off('change.attrbind', nil)
end
if @value && @value.reactive?
@value.remove
end
if @update_listener
@update_listener.remove
@update_listener = nil
end
@target = nil
@context = nil
@getter = nil
@value = nil
end
|
#remove_anchors ⇒ Object
117
118
119
|
# File 'lib/volt/page/bindings/attribute_binding.rb', line 117
def remove_anchors
raise "attribute bindings do not have anchors, can not remove them"
end
|
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
# File 'lib/volt/page/bindings/attribute_binding.rb', line 14
def setup
@value = value_from_getter(@getter)
update
if @value.reactive?
@update_listener = @value.on('changed') { update }
case @attribute_name
when 'value'
element.on('input.attrbind') { changed }
when 'checked'
element.on('change.attrbind') {|event| changed(event) }
end
end
end
|
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
# File 'lib/volt/page/bindings/attribute_binding.rb', line 50
def update
value = @value.cur
if @attribute_name == 'checked'
update_checked
return
end
if value.is_a?(NilMethodCall) || value.nil?
value = ''
end
self.value = value
end
|
#update_checked ⇒ Object
78
79
80
81
82
83
84
85
86
87
|
# File 'lib/volt/page/bindings/attribute_binding.rb', line 78
def update_checked
value = @value.cur
if value.is_a?(NilMethodCall) || value.nil?
value = false
end
element.prop('checked', value)
end
|
#value=(val) ⇒ Object
65
66
67
68
69
70
71
72
73
74
75
76
|
# File 'lib/volt/page/bindings/attribute_binding.rb', line 65
def value=(val)
case @attribute_name
when 'value'
if val != element.value
element.value = val
end
else
element[@attribute_name] = val
end
end
|