Bellhop Class
Abstract the communication layer between the iframe
and the parent DOM
connect
Bellhop
Parameters:
-
[iframe]
DOM
optional
The iframe to communicate with. If no value is set, the assumption
is that we're the child trying to communcate with our window.parent
-
[origin="*"]
String
optional
The domain to communicate with if different from the current.
Returns:
Bellhop:
Return instance of current object
destroy
()
Destroy and don't user after this
disconnect
()
Disconnect if there are any open connections
fetch
(
-
event
-
callback
-
[data]
-
[runOnce=false]
)
Bellhop
A convenience method for sending and the listening to create
a singular link to fetching data. This is the same calling send
and then getting a response right away with the same event.
Parameters:
-
event
String
-
callback
Function
The callback to call after, takes event object as one argument
-
[data]
Object
optional
Optional data to pass along
-
[runOnce=false]
Boolean
optional
If we only want to fetch once and then remove the listener
Returns:
Bellhop:
Return instance of current object
include
private
Parameters:
-
classname
String
Qualified class name as a string.
for example "cloudkid.MyClass" would return a reference
to the function window.cloudkid.MyClass.
off
Bellhop
Remove an event listener
Parameters:
-
type
String
The type of event to listen for. If undefined, remove all listeners.
-
[callback]
Function
optional
The optional handler when an event is triggered, if no callback
is set then all listeners by type are removed
Returns:
Bellhop:
Return instance of current object
on
(
-
type
-
callback
-
[priority=0]
)
Bellhop
Add an event listener to the listen to an event from either the parent or iframe
Parameters:
-
type
String | Object
The type of event to listen for or a map of events to callbacks.
Multiple events can be added by separating events with spaces.
-
callback
Function
The handler when an event is triggered
-
[priority=0]
Int
optional
The priority of the event listener. Higher numbers are handled first.
Returns:
Bellhop:
Return instance of current object
receive
()
private
Handle messages in the window
respond
(
-
event
-
data
-
[runOnce=false]
)
Bellhop
A convience method for listening to an event and then responding with some data
right away. Automatically removes the listener
Parameters:
-
event
String
-
data
Object
The object to pass back.
May also be a function; the return value will be sent as data in this case.
-
[runOnce=false]
Boolean
optional
If we only want to respond once and then remove the listener
Returns:
Bellhop:
Return instance of current object
reviver
Object
static
When restoring from JSON via JSON.parse
, we may pass a reviver function.
In our case, this will check if the object has a specially-named property (__classname
).
If it does, we will attempt to construct a new instance of that class, rather than using a
plain old Object. Note that this recurses through the object.
See JSON.parse()
Returns:
Object:
The object that was parsed - either cast to a class, or not
send
Bellhop
Send an event to the connected instance
Parameters:
-
event
String
The event type to send to the parent
-
[data]
Object
optional
Additional data to send along with event
Returns:
Bellhop:
Return instance of current object
toString
()
String
And override for the toString built-in method
Returns:
String:
Representation of this instance
trigger
Trigger any event handlers for an event type
Parameters:
-
event
Object
The event to send
_listeners
Object
private
The collection of event listeners
_sendLater
Array
private
Save any sends to wait until after we're done
connected
Boolean
private
If we are connected to another instance of the bellhop
Default: false
connecting
Boolean
private
If we are current trying to connec
Default: false
iframe
DOMElement
private
isChild
Boolean
private
If this instance represents an iframe instance
Default: true
onReceive
Function
private
Bound handler for the window message event
origin
Boolean
private
If using cross-domain, the domain to post to
Default: "*"
supported
Boolean
Do we have something to connect to, should be called after
attempting to connect()
target
DOM
private
The target where to send messages
connected
The connection has been established successfully
failed
Connection could not be established