Class: 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

#queue_update, #remove_anchors, #section, #value_from_getter

Constructor Details

#initialize(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
# File 'lib/volt/page/bindings/each_binding.rb', line 4

def initialize(target, context, binding_name, getter, variable_name, template_name)
  # puts "New EACH Binding"

  super(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

  @added_listener = @value.on('added') { |_, position, item| puts "ADDED" ; item_added(position) }
  @changed_listener = @value.on('changed') { puts "CHANGED" ; reload }
  @removed_listener = @value.on('removed') { |_, position| item_removed(position) }
end

Instance Method Details

#item_added(position) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/volt/page/bindings/each_binding.rb', line 70

def item_added(position)
  # ObjectTracker.enable_cache
  # puts "ADDED 1"
  binding_name = @@binding_number
  @@binding_number += 1

  # Setup new bindings in the spot we want to insert the item
  section.insert_anchor_before_end(binding_name)

  index = ReactiveValue.new(position)
  value = @value[index]
  
  item_context = SubContext.new({@item_name => value, :index => index, :parent => @value}, @context)

  # ObjectTracker.enable_cache
  @templates << TemplateRenderer.new(@target, item_context, binding_name, @template_name)
  # puts "ADDED 2"
  # ObjectTracker.disable_cache
end

#item_removed(position) ⇒ Object



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

def item_removed(position)
  position = position.cur
  @templates[position].remove_anchors
  @templates[position].remove
  @templates.delete_at(position)
  
  value_obj = @value.cur
  
  if value_obj
    size = value_obj.size - 1
  else
    size = 0
  end
  
  # puts "Position: #{position} to #{size}"
  
  # Removed at the position, update context for every item after this position
  position.upto(size) do |index|
    @templates[index].context.locals[:index].cur = index
  end
end

#reloadObject

When a change event comes through, its most likely upstream, so the whole array might have changed. In this case, just reload the whole thing TODO: Track to make sure the changed event isn’t being called too often (it is currently)



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/volt/page/bindings/each_binding.rb', line 28

def reload
  # ObjectTracker.enable_cache
  # Remove all of the current templates
  if @templates
    @templates.each do |template|
      template.remove_anchors
      
      # TODO: Make sure this is being removed since we already removed the anchors
      template.remove
    end
  end
  
  @templates = []
  
  # Run update again to rebuild
  update

  # ObjectTracker.disable_cache
end

#removeObject

When this each_binding is removed, cleanup.



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/volt/page/bindings/each_binding.rb', line 106

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(item = nil) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/volt/page/bindings/each_binding.rb', line 90

def update(item=nil)
  if item
    values = [item]
  else
    values = @value.cur
    return if values.is_a?(Model) || values.is_a?(Exception)
    values = values.attributes
  end

  # TODO: Switch to #each?
  values.each_with_index do |value,index|
    item_added(index)
  end
end