humhub.modules.client module provides some utilities for calling backend actions. The client logic is build on top of jquerys
$.ajax function and provides some additional functionality as a response wrapper, promises and enhanced error handling.
The client module provides the following functions for calling backend actions:
post- Send POST data to the server
get- Send a GET request to the server
html- Load HTML from server by GET
submit- Submit a form by POST
Note: By default all client functions except
dataType: jsonfrom the server. This behaviour can be changed by means of the ajax options.
The client module can be used as follows
The mentioned functions can handle the following argument combinations:
submit function requires either an form selector, form jquery node or an action event as first argument.
Note: Since Yii urls can't be created on client side, you'll have to inject them through data attributes or the module config.
Note: You should always add an
catchhandler, otherwise the actual error will be swallowed by the promise framework.
The response object returned by your client contains the following attributes:
url: the url of your call
status: the result status of the xhr object
response: the server response, either a json object or html depending of the 'dataType' setting of your call.
textStatus: In case of error: "timeout", "error", "abort", "parsererror", "application"
dataType: the expected response dataType of your call
xml: the response depending on dataType
If the response dataType is set to
json (default) and the server acutally returns json the response attributes will be accessible directly through the response wrapper object as in the following example
In case of an error (catch) the response furthermore contains
- error: parsed error returned by the server
- errorThrown: The textual portion of the HTTP status, such as "Not Found" or "Internal Server Error."
- validationError: is set to true if response status is 400
As of HumHub Version 1.2 Pjax is enabled by default and modules and themes should be held compatible with the single page approach.
You can disable Pjax by setting the following parameter in your
Disable Pjax for specific links
If Pjax is enabled, all regular links on your site will be attached with a pjax handler by default. The Pjax behaviour is not attached to links with the following attributes:
- data-pjax-prevent - explicit pjax prevent flag
- target - links with html target attributes
- data-target/data-toggle - bootstrap helper attributes
Beside using the
initOnPjaxLoad flag and
unload function mentioned in the Module Guide you can also listen to the following global events:
humhub:modules:client:pjax:beforeSend- is triggered before pjax page loads
humhub:modules:client:pjax:success- is triggered after a pjax page load
initfunction is called in case of a pjax request only if the modules
initOnPjaxLoadsetting is set to
Reload the current page
client.reload function should be used in case a page reload is required. If
Pjax is enabled, a
Pjax based page reload will be triggered unless the
preventPjax flag is set to true.