Trongate Website Homepage

Handling Request Errors

The mx-on-error attribute in Trongate MX allows you to specify an element that should be initialized or updated after a failed AJAX request. This feature is particularly useful for handling error scenarios and providing appropriate feedback or actions in your web application when a request doesn't succeed.

Syntax

<element mx-on-error="#error-element">

The value of mx-on-error should be a CSS selector that identifies the target element to be initialized or updated when an AJAX request fails.

Functionality

When an AJAX request initiated by Trongate MX fails, the framework checks for the presence of the mx-on-error attribute on the triggering element. If found, Trongate MX performs the following actions:

  1. Locates the target element specified by the CSS selector in the mx-on-error attribute.
  2. Triggers any page load events associated with the target element, effectively reinitializing it.

This process allows for dynamic error handling and provides a way to update the UI or perform additional actions when a request fails.

Use Cases

Example

<form mx-post="api/submit_data" 
      mx-target="#result-container"
      mx-on-error="#error-message">
  <!-- Form fields -->
  <button type="submit">Submit Data</button>
</form>

<div id="result-container"></div>

<div id="error-message" 
     mx-get="api/get_error_details" 
     mx-trigger="load">
  <!-- Error message content -->
</div>

In this example:

  1. If the form submission fails, the mx-on-error="#error-message" attribute is triggered.
  2. This initializes the #error-message element.
  3. The #error-message element has its own mx-get with mx-trigger="load", so it will fetch and display error details.

Best Practices

Additional Notes

By utilizing mx-on-error, you can create more robust and user-friendly Trongate MX applications that gracefully handle failed requests and provide appropriate feedback or alternative actions to your users.