Class: EachBinding
- Inherits:
-
BaseBinding
- Object
- BaseBinding
- EachBinding
- Defined in:
- lib/volt/page/bindings/each_binding.rb
Instance Attribute Summary
Attributes inherited from BaseBinding
#binding_name, #context, #target
Instance Method Summary collapse
- #current_values ⇒ Object
-
#initialize(page, target, context, binding_name, getter, variable_name, template_name) ⇒ EachBinding
constructor
A new instance of EachBinding.
- #item_added(position) ⇒ Object
- #item_removed(position) ⇒ Object
-
#reload ⇒ Object
When a changed event happens, we update to the new size.
-
#remove ⇒ Object
When this each_binding is removed, cleanup.
-
#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.
Methods inherited from BaseBinding
#queue_update, #remove_anchors, #section, #value_from_getter
Constructor Details
#initialize(page, target, context, binding_name, getter, variable_name, template_name) ⇒ EachBinding
Returns a new instance of EachBinding.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/volt/page/bindings/each_binding.rb', line 4 def initialize(page, target, context, binding_name, getter, variable_name, template_name) # puts "New EACH Binding" super(page, target, context, binding_name) @item_name = variable_name @template_name = template_name # Find the source for the content binding @value = value_from_getter(getter) @templates = [] # Run the initial render # update reload @added_listener = @value.on('added') { |_, position, item| item_added(position) } @changed_listener = @value.on('changed') { reload } @removed_listener = @value.on('removed') { |_, position| item_removed(position) } end |
Instance Method Details
#current_values ⇒ Object
91 92 93 94 95 96 97 98 |
# File 'lib/volt/page/bindings/each_binding.rb', line 91 def current_values values = @value.cur return [] if values.is_a?(Model) || values.is_a?(Exception) values = values.attributes return values end |
#item_added(position) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/volt/page/bindings/each_binding.rb', line 54 def item_added(position) # ObjectTracker.enable_cache # puts "ADDED 1" binding_name = @@binding_number @@binding_number += 1 if position >= @templates.size # Setup new bindings in the spot we want to insert the item section.insert_anchor_before_end(binding_name) else # Insert the item before an existing item section.insert_anchor_before(binding_name, @templates[position].binding_name) end index = ReactiveValue.new(position) value = @value[index] item_context = SubContext.new({@item_name => value, :index => index, :parent => @value}, @context) 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
44 45 46 47 48 49 50 51 52 |
# File 'lib/volt/page/bindings/each_binding.rb', line 44 def item_removed(position) position = position.cur @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 |
#reload ⇒ Object
When a changed event happens, we update to the new size.
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/volt/page/bindings/each_binding.rb', line 27 def reload # Adjust to the new size values = current_values templates_size = @templates.size values_size = values.size if templates_size < values_size (templates_size).upto(values_size-1) do |index| item_added(index) end elsif templates_size > values_size (templates_size-1).downto(values_size) do |index| item_removed(index) end end end |
#remove ⇒ Object
When this each_binding is removed, cleanup.
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/volt/page/bindings/each_binding.rb', line 102 def remove # puts "Remove Each" @added_listener.remove @added_listener = nil @changed_listener.remove @changed_listener = nil @removed_listener.remove @removed_listener = nil @templates.each(&:remove) @templates = nil 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.
81 82 83 84 85 86 87 88 89 |
# File 'lib/volt/page/bindings/each_binding.rb', line 81 def update_indexes_after(start_index) size = @templates.size if size > 0 start_index.upto(size-1) do |index| puts "UP INDEX: #{index}" @templates[index].context.locals[:index].cur = index end end end |