Module: Browser::EventTarget

Included in:
Element, FileList::File::FileReader
Defined in:
lib/browser/event_target.rb

Instance Method Summary collapse

Instance Method Details

#off(event_name, &block) ⇒ Object

Remove an event handler

Parameters:

  • event_name (String)

    the name of the event



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/browser/event_target.rb', line 27

def off event_name, &block
  if `#@native.removeEventListener !== undefined`
    `#@native.removeEventListener(event_name, block)`
  elsif `#@native.removeListener !== undefined`
    `#@native.removeListener(event_name, block)`
  else
    warn "[Browser] Not entirely sure how to remove an event listener from #{self}"
  end

  nil
end

#on(event_name) {|event| ... } ⇒ Proc

Add the block as a handler for the specified event name. Will use either ‘addEventListener` or `addListener` if they exist.

Parameters:

  • event_name (String)

    the name of the event

Yield Parameters:

Returns:

  • (Proc)

    the block to pass to ‘off` to remove this handler



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/browser/event_target.rb', line 9

def on event_name, &block
  wrapper = proc { |event| block.call Event.new(event) }

  if `#@native.addEventListener !== undefined`
    `#@native.addEventListener(event_name, wrapper)`
  elsif `#@native.addListener !== undefined`
    `#@native.addListener(event_name, wrapper)`
  else
    warn "[Browser] Not entirely sure how to add an event listener to #{self}"
  end

  wrapper
end