Class: DocumentEvents
Instance Method Summary collapse
- #add(event, binding, handler) ⇒ Object
- #handle(event_name, event, target) ⇒ Object
-
#initialize ⇒ DocumentEvents
constructor
A new instance of DocumentEvents.
- #remove(event, binding) ⇒ Object
Constructor Details
#initialize ⇒ DocumentEvents
Returns a new instance of DocumentEvents.
2 3 4 |
# File 'lib/volt/page/document_events.rb', line 2 def initialize @events = {} end |
Instance Method Details
#add(event, binding, handler) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/volt/page/document_events.rb', line 6 def add(event, binding, handler) # Track each document event based on the event, element id, then binding.object_id unless @events[event] # We haven't defined an event of type event yet, lets attach it to the # document. @events[event] = {} that = self %x{ $('body').on(event, function(e) { that.$handle(event, e, e.originalEvent.target); }); } end # puts "Register: #{event} - #{binding.binding_name} - #{binding.object_id}" @events[event][binding.binding_name] ||= {} @events[event][binding.binding_name][binding.object_id] = handler end |
#handle(event_name, event, target) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/volt/page/document_events.rb', line 30 def handle(event_name, event, target) # puts "Handle: #{event_name} on #{target}" element = Element.find(target) loop do # Lookup the handler, make sure to not assume the group # exists. # TODO: Sometimes the event doesn't exist, but we still get # an event. handlers = @events[event_name] handlers = handlers[element.id] if handlers if handlers handlers.values.each do |handler| # Call each handler for this object handler.call(event) end end if element.size == 0 break else element = element.parent end end nil end |
#remove(event, binding) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/volt/page/document_events.rb', line 59 def remove(event, binding) # Remove the event binding @events[event][binding.binding_name].delete(binding.object_id) # if there are no more handlers for this binding_name (the html id), then # we remove the binding name hash if @events[event][binding.binding_name].size == 0 @events[event].delete(binding.binding_name) end # if there are no more handlers in this event, we can unregister the event # from the document if @events[event].size == 0 @events.delete(event) # Remove the event from the body %x{ $('body').unbind(event); } end end |