Class: Volt::EachBinding
- Inherits:
-
BaseBinding
- Object
- BaseBinding
- Volt::EachBinding
- Defined in:
- lib/volt/page/bindings/each_binding.rb
Instance Attribute Summary
Attributes inherited from BaseBinding
#binding_name, #context, #target, #volt_app
Instance Method Summary collapse
- #current_values(values) ⇒ Object
-
#initialize(volt_app, target, context, binding_name, getter, variable_name, index_name, template_name) ⇒ EachBinding
constructor
A new instance of EachBinding.
- #item_added(position) ⇒ Object
- #item_removed(position) ⇒ Object
-
#remove ⇒ Object
When this each_binding is removed, cleanup.
- #remove_listeners ⇒ Object
-
#update(value) ⇒ Object
When a changed event happens, we update to the new size.
-
#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
#browser, #dom_section, #getter_fail, #remove_anchors
Constructor Details
#initialize(volt_app, target, context, binding_name, getter, variable_name, index_name, template_name) ⇒ EachBinding
Returns a new instance of EachBinding.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/volt/page/bindings/each_binding.rb', line 7 def initialize(volt_app, target, context, binding_name, getter, variable_name, index_name, template_name) super(volt_app, target, context, binding_name) @item_name = variable_name @index_name = index_name @template_name = template_name @templates = [] @getter = getter # Listen for changes @computation = lambda do begin value = @context.instance_eval(&@getter) rescue => e Volt.logger.error("EachBinding Error: #{e.inspect}") if RUBY_PLATFORM == 'opal' Volt.logger.error(`#{@getter}`) else Volt.logger.error(e.backtrace.join("\n")) end value = [] end value end.watch_and_resolve!( method(:update), method(:getter_fail) ) end |
Instance Method Details
#current_values(values) ⇒ Object
161 162 163 164 165 166 |
# File 'lib/volt/page/bindings/each_binding.rb', line 161 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
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 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/volt/page/bindings/each_binding.rb', line 93 def item_added(position) item_context = nil 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 do # Fetch only whats there currently, no promises. Volt.run_in_mode(:no_model_promises) do # puts "GET AT: #{item_context.locals[:_index_value]}" @value[item_context.locals[:_index_value]] end end 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}_dependency".to_sym] = value_dependency item_context.locals["#{@item_name}=".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(@volt_app, @target, item_context, binding_name, @template_name) @templates.insert(position, item_template) update_indexes_after(position) end |
#item_removed(position) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/volt/page/bindings/each_binding.rb', line 80 def item_removed(position) # Remove dependency @templates[position].context.locals[:_index_dependency].remove @templates[position].context.locals["_#{@item_name}_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 |
#remove ⇒ Object
When this each_binding is removed, cleanup.
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/volt/page/bindings/each_binding.rb', line 180 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_listeners ⇒ Object
168 169 170 171 172 173 174 175 176 177 |
# File 'lib/volt/page/bindings/each_binding.rb', line 168 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(value) ⇒ Object
When a changed event happens, we update to the new size.
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/volt/page/bindings/each_binding.rb', line 41 def update(value) # 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 = nil values_size = nil Volt.run_in_mode(:no_model_promises) do templates_size = @templates.size unless values.respond_to?(:size) fail InvalidObjectForEachBinding, "Each binding's require an object that responds to size and [] methods. The binding received: #{values.inspect}" end values_size = values.size end # 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 |
#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.
152 153 154 155 156 157 158 159 |
# File 'lib/volt/page/bindings/each_binding.rb', line 152 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 |