Class: AttributeBinding

Inherits:
BaseBinding show all
Defined in:
lib/volt/page/bindings/attribute_binding.rb

Instance Attribute Summary

Attributes inherited from BaseBinding

#binding_name, #context, #target

Instance Method Summary collapse

Methods inherited from BaseBinding

#queue_update, #remove_anchors, #section, #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
13
# File 'lib/volt/page/bindings/attribute_binding.rb', line 5

def initialize(page, target, context, binding_name, attribute_name, getter)
  # puts "New Attribute Binding: #{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



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/volt/page/bindings/attribute_binding.rb', line 34

def changed(event=nil)
  case @attribute_name
  when 'value'
    current_value = element.value
    # puts "NEW VAL: #{current_value} : #{@value.inspect}"
  else
    current_value = element.is(':checked')
  end

  @value.cur = current_value
end

#elementObject



46
47
48
# File 'lib/volt/page/bindings/attribute_binding.rb', line 46

def element
  Element.find('#' + binding_name)
end

#removeObject



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/volt/page/bindings/attribute_binding.rb', line 93

def remove
  # Unbind events, leave the element there since attribute bindings
  # aren't responsible for it being there.
  case @attribute_name
  when 'value'
    element.off('input.attrbind', nil)
  when 'checked'
    element.off('change.attrbind', nil)
  end
  
  if @update_listener
    @update_listener.remove
    @update_listener = nil
  end
  
  # Clear any references
  @target = nil
  @context = nil
  @section = nil
end

#setupObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/volt/page/bindings/attribute_binding.rb', line 15

def setup
  
  # Find the source for the content binding
  @value = value_from_getter(@getter)

  # Run the initial update (render)
  update

  @update_listener = @value.on('changed') { update }

  # Bind so when this value updates, we update
  case @attribute_name
  when 'value'
    element.on('input.attrbind') { changed }
  when 'checked'
    element.on('change.attrbind') {|event| changed(event) }
  end
end

#updateObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/volt/page/bindings/attribute_binding.rb', line 50

def update  
  if @attribute_target
    value = @attribute_target.to_html
  else
    value = @value.cur
  end

  if @attribute_name == 'checked'
    update_checked
    return
  end
  
  if value.is_a?(NilMethodCall) || value.nil?
    value = ''
  end

  self.value = value
end

#update_checkedObject



82
83
84
85
86
87
88
89
90
91
# File 'lib/volt/page/bindings/attribute_binding.rb', line 82

def update_checked
  value = @value.cur
  
  if value.is_a?(NilMethodCall) || value.nil?
    value = false
  end
  
  element.prop('checked', value)
  
end

#value=(val) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/volt/page/bindings/attribute_binding.rb', line 69

def value=(val)
  case @attribute_name
  when 'value'
    # TODO: only update if its not the same, this keeps it from moving the
    # cursor in text fields.
    if val != element.value
      element.value = val
    end
  else
    element[@attribute_name] = val
  end
end