Trongate Website Homepage

Handling Successful Requests

The mx-on-success attribute in Trongate MX allows you to specify an element that should be initialized or updated after a successful AJAX request. This feature is particularly useful for triggering additional actions or updates in your web application based on the success of a previous request.

Syntax

<element mx-on-success="#target-element">

The value of mx-on-success should be a CSS selector that identifies the target element to be initialized or updated upon successful completion of the AJAX request.

Functionality

When an AJAX request initiated by Trongate MX completes successfully, the framework checks for the presence of the mx-on-success 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-success attribute.
  2. Triggers any page load events associated with the target element, effectively reinitializing it.

This process allows for dynamic updates and chained actions based on successful AJAX operations.

Use Cases

Example

<form mx-post="api/submit_order" 
      mx-target="#order-confirmation"
      mx-on-success="#order-summary">
  <!-- Form fields -->
  <button type="submit">Place Order</button>
</form>

<div id="order-confirmation"></div>

<div id="order-summary" 
     mx-get="api/get_order_summary" 
     mx-trigger="load">
  <!-- Order summary content -->
</div>

In this example:

  1. When the form is submitted successfully, the response is inserted into #order-confirmation.
  2. The mx-on-success="#order-summary" attribute then triggers the initialization of the #order-summary element.
  3. Since #order-summary has its own mx-get with mx-trigger="load", it will fetch and display updated order summary information.

Best Practices

Additional Notes

By leveraging mx-on-success, you can create more dynamic and interconnected user interfaces in your Trongate MX applications, allowing for sophisticated update patterns with minimal JavaScript code.