Module: Apotomo::EventMethods
Overview
Introduces event-processing functions into the StatefulWidget.
Defined Under Namespace
Modules: ClassMethods
Instance Attribute Summary collapse
Instance Method Summary collapse
- #add_class_event_handlers ⇒ Object
-
#respond_to_event(type, options = {}) ⇒ Object
Instructs the widget to look out for type Events that are passing by while bubbling.
-
#trigger(*args) ⇒ Object
Fire an event of type and let it bubble up.
Instance Attribute Details
#page_updates ⇒ Object
14 15 16 |
# File 'lib/apotomo/event_methods.rb', line 14 def page_updates @page_updates ||= [] end |
Instance Method Details
#add_class_event_handlers ⇒ Object
18 19 20 |
# File 'lib/apotomo/event_methods.rb', line 18 def add_class_event_handlers(*) self.class..each { || respond_to_event(*) } end |
#respond_to_event(type, options = {}) ⇒ Object
Instructs the widget to look out for type Events that are passing by while bubbling. If an appropriate event is encountered the widget will send the targeted widget (or itself) to another state, which implies an update of the invoked widget.
You may configure the event handler with the following options:
:with => (optional) the state to invoke on the target widget, defaults to +type+.
:on => (optional) the targeted widget's id, defaults to <tt>self.name</tt>
:from => (optional) the source id of the widget that triggered the event, defaults to any widget
Example:
trap = (:trap, :charged, 'mouse_trap')
trap.respond_to_event :mouseOver, :with => :catch_mouse
This would instruct trap to catch a :mouseOver event from any widget (including itself) and to invoke the state :catch_mouse on itself as trigger.
hunter = (:form, :hunt_for_mice, 'my_form')
hunter << (:input_field, :smell_like_cheese, 'mouse_trap')
hunter << (:text_area, :stick_like_honey, 'bear_trap')
hunter.respond_to_event :captured, :from => 'mouse_trap', :with => :refill_cheese, :on => 'mouse_trap'
As both the bear- and the mouse trap can trigger a :captured event the later respond_to_event would invoke :refill_cheese on the mouse_trap widget as soon as this and only this widget fired. It is important to understand the :from parameter as it filters the event source - it wouldn't make sense to refill the mouse trap if the bear trap snapped, would it?
59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/apotomo/event_methods.rb', line 59 def respond_to_event(type, ={}) = .reverse_merge(:once => true, :with => type, :on => self.name) handler = InvokeEventHandler.new(:widget_id => [:on], :state => [:with]) return if [:once] and event_table.all_handlers_for(type, [:from]).include?(handler) on(type, :do => handler, :from => [:from]) end |
#trigger(*args) ⇒ Object
Fire an event of type and let it bubble up. You may add arbitrary payload data to the event.
Example:
trigger(:dropped, :area => 59)
which can be queried in a triggered state.
def on_drop(event)
if event[:area] == 59
81 82 83 |
# File 'lib/apotomo/event_methods.rb', line 81 def trigger(*args) fire(*args) end |