Sha256: c6c14142981cfbc1e501dfecf0e730b77731244d8debe1fb9675468fb905971e

Contents?: true

Size: 1.78 KB

Versions: 14

Compression:

Stored size: 1.78 KB

Contents

require 'volt/page/bindings/base_binding'

class IfBinding < BaseBinding
  def initialize(page, target, context, binding_name, branches)
    super(page, target, context, binding_name)

    getter, template_name = branches[0]

    @branches = []
    @listeners = []

    branches.each do |branch|
      getter, template_name = branch

      if getter.present?
        value = getter
      else
        # A nil value means this is an unconditional else branch, it
        # should always be true
        value = true
      end

      @branches << [value, template_name]
    end

    @computation = -> { update }.watch!
  end

  def update
    # Find the true branch
    true_template = nil
    @branches.each do |branch|
      value, template_name = branch

      if value.is_a?(Proc)
        begin
          current_value = @context.instance_eval(&value)
        rescue => e
          Volt.logger.error("IfBinding:#{object_id} error: #{e.inspect}\n" + `value.toString()`)
          current_value = false
        end
      else
        current_value = value
      end

      # TODO: A bug in opal requires us to check == true
      if current_value && !current_value.nil? && !current_value.is_a?(Exception)
        # 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(@page, @target, @context, binding_name, true_template)
      end
    end
  end

  def remove
    @computation.stop if @computation
    @computation = nil

    @template.remove if @template

    super
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
volt-0.8.14 lib/volt/page/bindings/if_binding.rb
volt-0.8.13 lib/volt/page/bindings/if_binding.rb
volt-0.8.11 lib/volt/page/bindings/if_binding.rb
volt-0.8.10 lib/volt/page/bindings/if_binding.rb
volt-0.8.9 lib/volt/page/bindings/if_binding.rb
volt-0.8.8 lib/volt/page/bindings/if_binding.rb
volt-0.8.7 lib/volt/page/bindings/if_binding.rb
volt-0.8.6 lib/volt/page/bindings/if_binding.rb
volt-0.8.5 lib/volt/page/bindings/if_binding.rb
volt-0.8.4 lib/volt/page/bindings/if_binding.rb
volt-0.8.3 lib/volt/page/bindings/if_binding.rb
volt-0.8.2 lib/volt/page/bindings/if_binding.rb
volt-0.8.1 lib/volt/page/bindings/if_binding.rb
volt-0.8.0 lib/volt/page/bindings/if_binding.rb