Class: Volt::EventBinding

Inherits:
BaseBinding show all
Defined in:
lib/volt/page/bindings/event_binding.rb

Instance Attribute Summary collapse

Attributes inherited from BaseBinding

#target, #volt_app

Instance Method Summary collapse

Methods inherited from BaseBinding

#dom_section, #getter_fail, #page, #remove_anchors

Constructor Details

#initialize(volt_app, target, context, binding_name, event_name, call_proc) ⇒ EventBinding

Returns a new instance of EventBinding.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/volt/page/bindings/event_binding.rb', line 32

def initialize(volt_app, target, context, binding_name, event_name, call_proc)
  super(volt_app, target, context, binding_name)
  @event_name = event_name

  handler = proc 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)

    # The following doesn't work due to the promise already chained issue.
    # # Ignore native objects.
    # result = nil unless BasicObject === result

    # # if the result is a promise, log an exception if it failed and wasn't
    # # handled
    # if result.is_a?(Promise) && !result.next
    #   result.fail do |err|
    #     Volt.logger.error("EventBinding Error: promise returned from event binding #{@event_name} was rejected")
    #     Volt.logger.error(err)
    #   end
    # end

  end

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

Instance Attribute Details

#binding_nameObject

Returns the value of attribute binding_name.



30
31
32
# File 'lib/volt/page/bindings/event_binding.rb', line 30

def binding_name
  @binding_name
end

#contextObject

Returns the value of attribute context.



30
31
32
# File 'lib/volt/page/bindings/event_binding.rb', line 30

def context
  @context
end

Instance Method Details

#removeObject

Remove the event binding



63
64
65
# File 'lib/volt/page/bindings/event_binding.rb', line 63

def remove
  page.events.remove(@event_name, self)
end