Sha256: e3664429ca7e168516ccbcb48a92964af9f983a49f35f3dd91da9c70c9726b63

Contents?: true

Size: 1.91 KB

Versions: 17

Compression:

Stored size: 1.91 KB

Contents

require 'volt/page/bindings/base_binding'

module Volt
  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
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
volt-0.8.27.beta3 lib/volt/page/bindings/if_binding.rb
volt-0.8.27.beta2 lib/volt/page/bindings/if_binding.rb
volt-0.8.27.beta1 lib/volt/page/bindings/if_binding.rb
volt-0.8.26.beta1 lib/volt/page/bindings/if_binding.rb
volt-0.8.26 lib/volt/page/bindings/if_binding.rb
volt-0.8.24 lib/volt/page/bindings/if_binding.rb
volt-0.8.23 lib/volt/page/bindings/if_binding.rb
volt-0.8.22 lib/volt/page/bindings/if_binding.rb
volt-0.8.22.beta2 lib/volt/page/bindings/if_binding.rb
volt-0.8.22.beta1 lib/volt/page/bindings/if_binding.rb
volt-0.8.21 lib/volt/page/bindings/if_binding.rb
volt-0.8.20 lib/volt/page/bindings/if_binding.rb
volt-0.8.19 lib/volt/page/bindings/if_binding.rb
volt-0.8.18 lib/volt/page/bindings/if_binding.rb
volt-0.8.17 lib/volt/page/bindings/if_binding.rb
volt-0.8.16 lib/volt/page/bindings/if_binding.rb
volt-0.8.15 lib/volt/page/bindings/if_binding.rb