Module: Fron::Eventable

Extended by:
Eventable
Included in:
Eventable
Defined in:
opal/fron/core/eventable.rb

Overview

Eventable

Instance Method Summary collapse

Instance Method Details

#off(event = nil, &block) ⇒ Object

Removes event listeners


39
40
41
42
43
44
45
46
47
48
49
50
# File 'opal/fron/core/eventable.rb', line 39

def off(event = nil, &block)
  return unless @events
  if block_given?
    @events[event].delete block
  elsif event
    @events[event] = []
  else
    @events.keys.each do |key|
      @events.delete key
    end
  end
end

#on(event, &block) ⇒ Proc

Add an event listener


14
15
16
17
18
19
# File 'opal/fron/core/eventable.rb', line 14

def on(event, &block)
  @events ||= {}
  @events[event] ||= []
  @events[event] << block
  block
end

#trigger(event, data = {}, triggerGlobal = true) ⇒ Object

Triggers an event


26
27
28
29
30
31
32
33
# File 'opal/fron/core/eventable.rb', line 26

def trigger(event, data = {}, triggerGlobal = true)
  return unless @events
  return unless @events[event]
  Eventable.trigger event, data, false if triggerGlobal && self != Fron::Eventable
  @events[event].each do |block|
    block.call data
  end
end