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

Returns a new instance of EachBinding.



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

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

  @item_name     = variable_name
  @index_name    = index_name
  @template_name = template_name

  @templates = []

  @getter      = getter

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

Instance Method Details

#current_values(values) ⇒ Object



131
132
133
134
135
136
# File 'lib/volt/page/bindings/each_binding.rb', line 131

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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# 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


  # Get and set value
  value_dependency                    = Dependency.new
  item_context.locals["_#{@item_name.to_s}_dependency".to_sym] = value_dependency

  item_context.locals["#{@item_name.to_s}=".to_sym] = proc do |val|
    value_dependency.changed!
    @value[item_context.locals[:_index_value]] = val
  end

  # If the user provides an each_with_index, we can assign the lookup for the index
  # variable here.
  if @index_name
    item_context.locals[@index_name.to_sym] = proc do
      position_dependency.depend
      item_context.locals[:_index_value]
    end
  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
  @templates[position].context.locals["_#{@item_name.to_s}_dependency".to_sym].remove

  @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.



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 21

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
    # Adjust to the new size
    values = current_values(value)

    @value = values

    remove_listeners

    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.



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/volt/page/bindings/each_binding.rb', line 150

def remove
  @computation.stop
  @computation = nil

  # Clear value
  @value       = []

  @getter = nil

  remove_listeners

  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

#remove_listenersObject



138
139
140
141
142
143
144
145
146
147
# File 'lib/volt/page/bindings/each_binding.rb', line 138

def remove_listeners
  if @added_listener
    @added_listener.remove
    @added_listener = nil
  end
  if @removed_listener
    @removed_listener.remove
    @removed_listener = nil
  end
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.



122
123
124
125
126
127
128
129
# File 'lib/volt/page/bindings/each_binding.rb', line 122

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