Stay Class
The Stay XHR System.
Used for requesting page content asynchronously while staying on the same page.
Each request can have a hard timeout to avoid endless loading times that are often deemed to fail anyways.
Constructor
Stay
-
[options]
Parameters:
-
[options]
Object optional- The options.
-
[stderr=console]
Array optional- The standard output for error messages.
-
[infix="/json"]
String optional- The special url pattern infix for the asynchronous content requests.
-
[timeoutPost=60000]
Number optional- Hard timeout for POST. 0 means no timeout.
-
[timeoutGet=5000]
Number optional- Hard timeout for GET. 0 means no timeout.
-
[autoUpdate=true]
Boolean optional- Whether Stay should automatically update the page content.
Throws:
An error is thrown if asynchronous requests are not supported.
Item Index
Methods
Properties
Methods
_handleResponse
-
xhr
This function acts when the xhr object changes its readyState. The response will be a JSON object or an error page. Anything else will be caught as a JSON parse exception and announced in stderr.
Parameters:
-
xhr
XMLHttpRequest- The xhr object that fired the event.
_switchPage
-
event
This function is bound to all links and forms and executes the desired page navigation on left clicks.
Parameters:
-
event
Event- The event.
_updateListeners
()
private
Binds event listeners to all links and forms. This method is combined with the cleanup and basically refreshes the navigation listeners.
_updateView
-
response
Updates the containers with the new data.
Parameters:
-
response
Object- The properties of the response object correspond with the target DOM containers.
getPathname
-
url
Uses the native browser parsing mechanism to retrieve the pathname of a url.
Parameters:
-
url
String- The URL to parse.
Returns:
The pathname.
handleBackForward
-
event
Support browser functionality "back" and "forward". Depends on the boolean variable "locked" in order to determine whether this navigation should be executed. The "backForward" flag tells the system that the next state mustn't be pushed.
Parameters:
-
event
Object- The event.
handleResponse
-
event
Triggers the internal response handler.
Parameters:
-
event
Object- The event.
handleTimeout
()
private
Handles xhr timeouts, ignores the event object.
unbindListeners
()
If you want to destroy Stay, you should call this method before you drop your reference to the Stay instance.
update
-
response
Updates the view, the navigation listeners and the history state. Also emits an event to signilise that the page has been loaded.
The update function needs to be called after each navigation in order to unlock the system! This happens by default, but that behaviour can be disabled. It is then the responsibility of the programmer to call stay.update(response) with the response data provided by the "receive" event.
Parameters:
-
response
Object- The response to display.
Properties
absolutePath
String
private
The current absolute path.
autoUpdate
Boolean
Auto update flag.
backForward
Boolean
private
Back-forward flag.
containers
Array
private
A list of references to the response field DOM elements.
Error
Object
private
final
static
Enumeration of Error Messages.
exclusions
Array
Regular expressions for excluded URIs.
historyState
Object
private
The current history state. Can't rely on history.state right now.
infix
String
The infix to use for the asynchronous requests.
intermediateContainer
HTMLDivElement
private
A container which is filled by setting its innerHTML. The created DOM elements are taken from this container and appended to the response fields.
local
RegExp
private
Regular expression to check if a url is local.
locked
Boolean
private
Lock flag.
stderr
String
The standard error output. This string represents the ID of the target DOM container which should hold any error messages. If none is specified, errors will be logged to the console. A request timeout is considered an error, for example.
timeoutGet
Number
GET timeout.
timeoutPost
Number
POST timeout.
xhr
XMLHttpRequest
private
The internal XMLHttpRequest instance.
Events
error
Returns XmlHttpRequest errors.
load
Signalises that a page update has finished.
receive
Returns the parsed server response.
Event Payload:
-
response
Object- The server response, ready to be inserted into the respective response fields.
-
status
Number- The status of the xhr response.