Sha256: 492b9b638efc6c6d610cb92f4328622696f7ae4f681d8d842c9d2c4a02b4d5bc

Contents?: true

Size: 1.07 KB

Versions: 3

Compression:

Stored size: 1.07 KB

Contents

require 'glimmer/data_binding/observable'
require 'glimmer/data_binding/observer'

module Glimmer
  module DataBinding
    class ElementBinding
      include Observable
      include Observer

      attr_reader :element, :property
      def initialize(element, property, translator = nil)
        @element = element
        @property = property
        @translator = translator

        # TODO implement automatic cleanup upon calling element.remove
        # Alternatively, have this be built into ElementProxy and remove this code
#         if @element.respond_to?(:dispose)
#           @element.on_widget_disposed do |dispose_event|
#             unregister_all_observables
#           end
#         end
      end
      
      def call(value)
        evaluated_property_value = evaluate_property
        converted_value = @translator&.call(value, evaluated_property_value) || value
        @element.send("#{@property}=", converted_value) unless converted_value == evaluated_property_value
      end
      
      def evaluate_property
        @element.send(@property)
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
glimmer-dsl-web-0.4.1 lib/glimmer/data_binding/element_binding.rb
glimmer-dsl-web-0.4.0 lib/glimmer/data_binding/element_binding.rb
glimmer-dsl-web-0.3.2 lib/glimmer/data_binding/element_binding.rb