Sha256: da2d756879418b8bb60bc5f14278de6a0cdafa8f624063c2529ac6d1252734f6

Contents?: true

Size: 1.7 KB

Versions: 36

Compression:

Stored size: 1.7 KB

Contents

require 'volt/page/bindings/base_binding'

class IfBinding < BaseBinding
  def initialize(target, context, binding_name, branches)
    getter, template_name = branches[0]
    # puts "New If Binding: #{binding_name}, #{getter.inspect}"


    super(target, context, binding_name)

    @branches = []
    @listeners = []
    
    branches.each do |branch|
      getter, template_name = branch
      
      if getter.present?
        # Lookup the value
        value = value_from_getter(getter)

        if value.reactive?
          # Trigger change when value changes
          @listeners << value.on('changed') { update }
        end
      else
        # A nil value means this is an unconditional else branch, it
        # should always be true
        value = true
      end
      
      @branches << [value, template_name]
    end
    
    update
  end
  
  def update
    # Find the true branch
    true_template = nil
    @branches.each do |branch|
      value, template_name = branch
      
      # TODO: A bug in opal requires us to check == true
      if value.cur.true? == true
        # This branch is currently true
        true_template = template_name
        break
      end
    end
    
    # Change out the template only if the true branch has changed.
    if @last_true_template != true_template
      @last_true_template = true_template
      
      if @template
        @template.remove
        @template = nil
      end
      
      if true_template
        @template = TemplateRenderer.new(@target, @context, binding_name, true_template)
      end
    end
  end
  
  def remove
    # Remove all listeners on any reactive values
    @listeners.each(&:remove)

    @template.remove if @template
    
    super
  end
end

Version data entries

36 entries across 36 versions & 1 rubygems

Version Path
volt-0.5.18 lib/volt/page/bindings/if_binding.rb
volt-0.5.17 lib/volt/page/bindings/if_binding.rb
volt-0.5.16 lib/volt/page/bindings/if_binding.rb
volt-0.5.15 lib/volt/page/bindings/if_binding.rb
volt-0.5.14 lib/volt/page/bindings/if_binding.rb
volt-0.5.13 lib/volt/page/bindings/if_binding.rb
volt-0.5.12 lib/volt/page/bindings/if_binding.rb
volt-0.5.11 lib/volt/page/bindings/if_binding.rb
volt-0.5.10 lib/volt/page/bindings/if_binding.rb
volt-0.5.9 lib/volt/page/bindings/if_binding.rb
volt-0.5.8 lib/volt/page/bindings/if_binding.rb
volt-0.5.7 lib/volt/page/bindings/if_binding.rb
volt-0.5.6 lib/volt/page/bindings/if_binding.rb
volt-0.5.4 lib/volt/page/bindings/if_binding.rb
volt-0.5.3 lib/volt/page/bindings/if_binding.rb
volt-0.5.2 lib/volt/page/bindings/if_binding.rb
volt-0.5.1 lib/volt/page/bindings/if_binding.rb
volt-0.5.0 lib/volt/page/bindings/if_binding.rb
volt-0.4.18 lib/volt/page/bindings/if_binding.rb
volt-0.4.17 lib/volt/page/bindings/if_binding.rb