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

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseBinding

#browser, #dom_section, #getter_fail, #remove_anchors

Constructor Details

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

Returns a new instance of EventBinding.



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
61
62
63
64
65
66
67
# File 'lib/volt/page/bindings/event_binding.rb', line 35

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

  # Map blur/focus to focusout/focusin
  @event_name = case event_name
  when 'blur'
    'focusout'
  when 'focus'
    'focusin'
  else
    event_name
  end


  handler = proc do |js_event, *args|
    event = JSEvent.new(js_event)
    event.prevent_default! if event_name == 'submit'

    # When the event is triggered via ```trigger(..)``` in a controller,
    # it will pass its self as the first argument.  We set that to
    # ```controller``` on the event, so it can be easily accessed.
    if args[0].is_a?(Volt::ModelController)
      args = args.dup
      event.controller = args.shift
    end

    args << event

    self.class.call_handler_proc(@context, call_proc, event, args)
  end

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

Instance Attribute Details

#binding_nameObject

Returns the value of attribute binding_name.



33
34
35
# File 'lib/volt/page/bindings/event_binding.rb', line 33

def binding_name
  @binding_name
end

#contextObject

Returns the value of attribute context.



33
34
35
# File 'lib/volt/page/bindings/event_binding.rb', line 33

def context
  @context
end

Class Method Details

.call_handler_proc(context, call_proc, event, args) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/volt/page/bindings/event_binding.rb', line 69

def self.call_handler_proc(context, call_proc, event, args)
  # When the EventBinding is compiled, it converts a passed in string to
  # get a Method:
  #
  # Example:
  #   <a e-awesome="some_method">...</a>
  #
  # The call_proc will be passed in as:  Proc.new { method(:some_method) }
  #
  # So first we call the call_proc, then that returns a method (or proc),
  # which we call passing in the arguments based on the arity.
  #
  # If the e- binding has arguments passed to it, we just use those.
  result = context.instance_exec(event, &call_proc)
  # Trim args to match arity

  # The proc returned a
  if result && result.is_a?(Method)
    args = args[0...result.arity]

    result.call(*args)
  end

  result
end

Instance Method Details

#removeObject

Remove the event binding



96
97
98
# File 'lib/volt/page/bindings/event_binding.rb', line 96

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