Class: EventdElement

Inherits:
EventdObject show all
Defined in:
lib/eventd/eventd_element.rb

Overview

EventdElement

Object representation of an element on the client’s page. Mimics a jQuery style API.

Shot Framework - Copyright © Jesse Aaron Dunlap <[email protected]> Licensed under the MIT License. For full licensing information, please see LICENSE.md. github.com/JesseDunlap/shot/

Instance Attribute Summary collapse

Attributes inherited from EventdObject

#events

Instance Method Summary collapse

Methods inherited from EventdObject

#emit, #off

Constructor Details

#initialize(selector, client) ⇒ EventdElement

Initializer

Attributes

  • selector

  • client



22
23
24
25
26
# File 'lib/eventd/eventd_element.rb', line 22

def initialize(selector, client)
	super()
	@selector = selector
	@client = client
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *attributes, &block) ⇒ Object

Handles any method called, and attempts to turn it into a valid jQuery call



30
31
32
33
# File 'lib/eventd/eventd_element.rb', line 30

def method_missing(method, *attributes, &block)
	self.jq method, attributes
	return self
end

Instance Attribute Details

#clientObject

Instance of EventdClient, which is used to trigger interface emissions



11
12
13
# File 'lib/eventd/eventd_element.rb', line 11

def client
  @client
end

#selectorObject

jQuery Selector associated with the element



14
15
16
# File 'lib/eventd/eventd_element.rb', line 14

def selector
  @selector
end

Instance Method Details

#jq(method, *attributes) ⇒ Object

Call a jQuery method

Attribtues

  • method

  • *attributes



41
42
43
44
45
46
47
# File 'lib/eventd/eventd_element.rb', line 41

def jq(method, *attributes)
	@client.emit "eapi_jq", {
		:selector => @selector,
		:method => method,
		:attributes => attributes
	}
end

#on(event, &callback) ⇒ Object

Add an event listener

Attributes

  • event

  • &callback



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/eventd/eventd_element.rb', line 55

def on(event, &callback)
	random_callback_id = SecureRandom.uuid
	
	@client.emit "eapi_event", {
		:selector => @selector,
		:callback => random_callback_id,
		:event => event
	}

	@client.on random_callback_id do
		callback.call
	end

	super(event, callback)
end