Sha256: 606ed8de1dc26ef01c60ab8e2fed7731bddb270cfd003fd49a640e887ba1459e

Contents?: true

Size: 1.1 KB

Versions: 23

Compression:

Stored size: 1.1 KB

Contents

require 'volt/page/bindings/base_binding'

# TODO: We need to figure out how we want to wrap JS events
class JSEvent
  attr_reader :js_event
  def initialize(js_event)
    @js_event = js_event
  end

  def key_code
    `this.js_event.keyCode`
  end

  def stop!
    `this.js_event.stopPropagation();`
  end

  def prevent_default!
    `this.js_event.preventDefault();`
  end

  def target
    `this.js_event.toElement`
  end
end


class EventBinding < BaseBinding
  attr_accessor :context, :binding_name
  def initialize(page, target, context, binding_name, event_name, call_proc)
    super(page, target, context, binding_name)
    @event_name = event_name

    handler = Proc.new do |js_event|
      event = JSEvent.new(js_event)
      event.prevent_default! if event_name == 'submit'

      # Call the proc the user setup for the event in context,
      # pass in the wrapper for the JS event
      result = @context.instance_exec(event, &call_proc)
    end

    @listener = @page.events.add(event_name, self, handler)
  end

  # Remove the event binding
  def remove
    @page.events.remove(@event_name, self)
  end
end

Version data entries

23 entries across 23 versions & 1 rubygems

Version Path
volt-0.8.14 lib/volt/page/bindings/event_binding.rb
volt-0.8.13 lib/volt/page/bindings/event_binding.rb
volt-0.8.11 lib/volt/page/bindings/event_binding.rb
volt-0.8.10 lib/volt/page/bindings/event_binding.rb
volt-0.8.9 lib/volt/page/bindings/event_binding.rb
volt-0.8.8 lib/volt/page/bindings/event_binding.rb
volt-0.8.7 lib/volt/page/bindings/event_binding.rb
volt-0.8.6 lib/volt/page/bindings/event_binding.rb
volt-0.8.5 lib/volt/page/bindings/event_binding.rb
volt-0.8.4 lib/volt/page/bindings/event_binding.rb
volt-0.8.3 lib/volt/page/bindings/event_binding.rb
volt-0.8.2 lib/volt/page/bindings/event_binding.rb
volt-0.8.1 lib/volt/page/bindings/event_binding.rb
volt-0.8.0 lib/volt/page/bindings/event_binding.rb
volt-0.7.23 lib/volt/page/bindings/event_binding.rb
volt-0.7.22 lib/volt/page/bindings/event_binding.rb
volt-0.7.21 lib/volt/page/bindings/event_binding.rb
volt-0.7.20 lib/volt/page/bindings/event_binding.rb
volt-0.7.19 lib/volt/page/bindings/event_binding.rb
volt-0.7.18 lib/volt/page/bindings/event_binding.rb