Dynamic Application API
spa()
asyncNavigation without reloading the page, with either full or partial content updates.
-
Accepts an object with the following properties:
PropertyTypeDescriptionpathstringURL path of the new page.spastringThe key of the object used from the "spa" property in the "config.js" configuration file for the new page.componentstring
objectThe name of a component or an object containing components dynamically used on the new page.apiobjectThe object that is used by the new page to make the API request.
More details about the API object can be found in the documentation for the api() interface.itDOM elementA DOM element associated with navigation, which can additionally have the following attributes:AttributeTypeDescriptiondata-spa-apiobjectThe object to be used for making the API request.
More details about the API object can be found in the documentation for the api() interface.
Example: data-spa-api="url: '/api', method: 'POST'"data-spa-updateobject
array
DOM elementAn object where the keys represent the property names from the API response whose content needs to be output, and the values are the DOM elements relative to which this content should be output.
If the content is nested, dot notation can be used to access the desired content, such as "response.user.name".
An array of DOM elements, relative to which the content will be output if the request returns content immediately.
A DOM element relative to which the content will be output if the request returns content immediately.
If an attribute is null, DOM elements with "data-it" attributes will be used as placeholders for replacement. The values of these attributes should match the keys from the API response, such as "data-it="name"" or "data-it="response.user.name"".
If a <head> element is specified for content replacement, it will be automatically disassembled and reassembled to avoid visual breaks.
Existing CSS styles will remain intact and will not be duplicated, even if they are present in the replacement data.
Example: data-spa-update="body: $('#view'), target: null"data-spa-loaderDOM element
arrayThe element or an array of elements that handles the loaders.
For the specified element, the aria-busy="true" attribute will be automatically set during the request and changed to aria-busy="false" once the request is complete.
To control the display of blocks, assign the necessary CSS styles to the relevant attributes.
Example: data-spa-loader="$('#loader')"data-spa-modestring
object
booleanIf content needs to be replaced, it must be false.
Otherwise, it must be one of the following values, indicating where the content will be output:ArgumentDescription'before'Places the content before the matched DOM element (before the opening tag).'after'Places the content after the matched DOM element (after the closing tag).'begin'Places the content immediately after the matched DOM element's opening tag (before the first child).'end'Places the content right before the matched DOM element's closing tag (after the last child).
To assign a specific option to each block, use an object, e.g., {main: 'end', 'response.user.name': false}.
If this attribute is specified and not set to false, DOM elements with the "data-remove" attribute matching existing DOM elements will be removed.
This is useful for scenarios such as adding new content and removing outdated elements, like replacing old pagination with updated content.
Example: data-spa-mode="end"data-spa-scrollobject
booleanThe object to use for scrolling.
More details on the scroll object can be found in the documentation for the toScroll() method.
To specify the DOM element that needs to be scrolled, the "it" property can be used.
If true, scrolling will be done to the top with default settings.
Example: data-spa-scroll="speed: 300, it: $('#view'), target: null" - When updating the URL to a similar one, it doesn't create duplicates but instead updates the data associated with the current URL.
- Returns true on success; otherwise, returns false.
- It is not necessary to wait for the method to be executed using await if not needed.
Example:
// Navigation is based on the underlying configuration object.
spa({path: '/page'});
// Navigation is based on the underlying configuration object with optional overrides.
spa({it: $('a[href="/page"]')});
// Performs navigation by appending additional parameters.
spa({
path: '/page',
spa: 'main',
component: 'navigation',
api: {
url: '/api',
method: 'POST',
headers: false,
body: {
method: 'getPage',
path: '/page'
},
format: 'json'
}
});
// Waits for the method to complete its execution.
await spa({path: '/page'});