Class: Volt::EachBinding

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

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, getter, variable_name, template_name) ⇒ EachBinding

Returns a new instance of EachBinding.



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/volt/page/bindings/each_binding.rb', line 5

def initialize(page, target, context, binding_name, getter, variable_name, template_name)
  super(page, target, context, binding_name)

  @item_name     = variable_name
  @template_name = template_name

  @templates = []

  @getter      = getter

  # Listen for changes
  @computation = -> { reload }.watch!
end

Instance Method Details

#current_values(values) ⇒ Object



117
118
119
120
121
122
# File 'lib/volt/page/bindings/each_binding.rb', line 117

def current_values(values)
  return [] if values.is_a?(Model) || values.is_a?(Exception)
  values = values.attributes if values.respond_to?(:attributes)

  values
end

#item_added(position) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/volt/page/bindings/each_binding.rb', line 70

def item_added(position)
  binding_name     = @@binding_number
  @@binding_number += 1

  if position >= @templates.size
    # Setup new bindings in the spot we want to insert the item
    dom_section.insert_anchor_before_end(binding_name)
  else
    # Insert the item before an existing item
    dom_section.insert_anchor_before(binding_name, @templates[position].binding_name)
  end

  # TODORW: :parent => @value may change
  item_context                           = SubContext.new({ _index_value: position, parent: @value }, @context)
  item_context.locals[@item_name.to_sym] = proc { @value[item_context.locals[:_index_value]] }

  position_dependency                    = Dependency.new
  item_context.locals[:index_dependency] = position_dependency

  # Get and set index
  item_context.locals[:index=]           = proc do |val|
    position_dependency.changed!
    item_context.locals[:_index_value] = val
  end

  item_context.locals[:index] = proc do
    position_dependency.depend
    item_context.locals[:_index_value]
  end

  item_template = TemplateRenderer.new(@page, @target, item_context, binding_name, @template_name)
  @templates.insert(position, item_template)

  update_indexes_after(position)
end

#item_removed(position) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/volt/page/bindings/each_binding.rb', line 57

def item_removed(position)
  # Remove dependency
  @templates[position].context.locals[:index_dependency].remove

  # puts "REMOVE AT: #{position.inspect} - #{@templates[position].inspect} - #{@templates.inspect}"
  @templates[position].remove_anchors
  @templates[position].remove
  @templates.delete_at(position)

  # Removed at the position, update context for every item after this position
  update_indexes_after(position)
end

#reloadObject

When a changed event happens, we update to the new size.



20
21
22
23
24
25
26
27
28
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
# File 'lib/volt/page/bindings/each_binding.rb', line 20

def reload
  begin
    value = @context.instance_eval(&@getter)
  rescue => e
    Volt.logger.error("EachBinding Error: #{e.inspect}")
    value = []
  end

  # Since we're checking things like size, we don't want this to be re-triggered on a
  # size change, so we run without tracking.
  Computation.run_without_tracking do
    # puts "RELOAD:-------------- #{value.inspect}"
    # Adjust to the new size
    values = current_values(value)
    @value = values

    @added_listener.remove if @added_listener
    @removed_listener.remove if @removed_listener

    if @value.respond_to?(:on)
      @added_listener   = @value.on('added') { |position| item_added(position) }
      @removed_listener = @value.on('removed') { |position| item_removed(position) }
    end

    templates_size = @templates.size
    values_size    = values.size

    # Start over, re-create all nodes
    (templates_size - 1).downto(0) do |index|
      item_removed(index)
    end
    0.upto(values_size - 1) do |index|
      item_added(index)
    end
  end
end

#removeObject

When this each_binding is removed, cleanup.



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/volt/page/bindings/each_binding.rb', line 125

def remove
  @computation.stop
  @computation = nil

  # Clear value
  @value       = nil

  if @added_listener
    @added_listener.remove
    @added_listener = nil
  end

  if @removed_listener
    @removed_listener.remove
    @removed_listener = nil
  end

  if @templates
    template_count = @templates.size
    template_count.times do |index|
      item_removed(template_count - index - 1)
    end
    # @templates.compact.each(&:remove)
    @templates = nil
  end

  super
end

#update_indexes_after(start_index) ⇒ Object

When items are added or removed in the middle of the list, we need to update each templates index value.



108
109
110
111
112
113
114
115
# File 'lib/volt/page/bindings/each_binding.rb', line 108

def update_indexes_after(start_index)
  size = @templates.size
  if size > 0
    start_index.upto(size - 1) do |index|
      @templates[index].context.locals[:index=].call(index)
    end
  end
end