Class: Roda::Component::Events

Inherits:
Struct
  • Object
show all
Defined in:
lib/roda/component/events.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#component_opts ⇒ Object

Returns the value of attribute component_opts

Returns:

  • (Object) —

    the current value of component_opts



3
4
5
# File 'lib/roda/component/events.rb', line 3

def component_opts
  @component_opts
end

#klass ⇒ Object

Returns the value of attribute klass

Returns:

  • (Object) —

    the current value of klass



3
4
5
# File 'lib/roda/component/events.rb', line 3

def klass
  @klass
end

#request ⇒ Object

Returns the value of attribute request

Returns:

  • (Object) —

    the current value of request



3
4
5
# File 'lib/roda/component/events.rb', line 3

def request
  @request
end

#scope ⇒ Object

Returns the value of attribute scope

Returns:

  • (Object) —

    the current value of scope



3
4
5
# File 'lib/roda/component/events.rb', line 3

def scope
  @scope
end

Instance Method Details

#on(name, options = {}, &block) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/roda/component/events.rb', line 4

def on name, options = {}, &block
  if client? && options.is_a?(String)
    class_name   = klass._name
    class_events = (events[class_name] ||= {})
    event        = (class_events[:_jquery_events] ||= [])
    event        << [block, class_name, options, name]
  elsif options.is_a?(Hash)
    limit_if = options.delete(:if) || []
    limit_if = [limit_if] unless limit_if.is_a? Array

    class_name   = options.delete(:for) || klass._name
    class_events = (events[class_name] ||= {})
    event        = (class_events[name.to_s] ||= [])

    # remove the type, if we have an on if and it isn't in the engine_type
    if limit_if.any? && !limit_if.include?(engine_type.to_sym)
      block = Proc.new {}
    end

    event << [block, klass._name, options]
  end
end

#trigger(name, options = {}) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/roda/component/events.rb', line 45

def trigger name, options = {}
  content = ''
  e = events[klass._name]

  return unless e

  (e[name.to_s] || []).each do |event|
    block, comp, opts = event

    if !opts[:socket]
      response = Component::Instance.new(component(comp), scope).instance_exec options, &block

      if response.is_a? Roda::Component::DOM
        content = response.to_xml
      else
        content = response
      end
    else
      $faye.publish("/components/#{klass._name}", {
        name: klass._name,
        type: 'event',
        event_type: 'call',
        event_method: name.to_s,
        data: options
      })
    end
  end

  content
end

#trigger_jquery_events ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/roda/component/events.rb', line 27

def trigger_jquery_events
  return unless e = events[klass._name]

  (e[:_jquery_events] || []).each do |event|
    block, comp, selector, name = event

    name = name.to_s

    if name != 'ready'
      Document.on name, selector do |evt|
        Component::Instance.new(component(comp), scope).instance_exec evt.current_target, evt, &block
      end
    else
      Component::Instance.new(component(comp), scope).instance_exec Document.find(selector), nil, &block
    end
  end
end