Javascript
Javascript module
This extension provides a Javascript module that you can import in your own Javascript code to make the request and get the parsed page response.
The Frontend class provides a request method which expects an object with the following properties:
page: The ID of the page you want to request.Id language: The ID of the language you want to request the page in.Id additional: (optional) A string with the additional get parameters to be sent with the request.Get Vars
Example usage:
import FrontendRequest from "@maxserv/frontend-request/frontend-request.js"
class YourJavascriptClass {
async fetchPage(pageId, languageId) {
FrontendRequest.request({
pageId: pageId,
languageId: languageId,
}).then((response) => {
// Handle the response here
console.log(response);
}).catch((error) => {
// Handle any errors here
console.error(error);
})
}
}
Copied!
Fire event to listen to
The FrontendRequest class also has a method request which you can call from within PHP to
fire an event that can be listened to in your JavaScript code.
An example using the Javascript within the Page:
$pageRenderer->getJavaScriptRenderer()->addJavaScriptModuleInstruction(
JavaScriptModuleInstruction::create('@maxserv/frontend-request/frontend-request.js')->invoke(
'requestWithEvent',
[
'pageId' => $pageId,
'languageId' => $languageId,
'additionalGetVars' => $additionalGetVars ?? '',
],
'your-event-name'
)
);
Copied!
This will fire an event with the name your- that you can listen to in your JavaScript code.
document.addEventListener('your-event-name', (event) => {
// Handle the event here
console.log(event.detail);
});
Copied!