JavaScript CookBook/AJAX Interactions/Executing Javascript commands after an AJAX-based function

From GeoMedia Smart Client
Jump to: navigation, search
GMSC 2013/2014 Javascript API Overview Form Events Types Utility Functions client-side Validation Conditions Interaction with SmartClient Cookbook Cheatsheet

Executing Javascript commands after an AJAX-based function

Problem: You need to execute server-side triggers via IG.trigger() or save form data on the server using IG.executeRequest(). You want to manipulate the form AFTER these requests have successfully finished.

Solution: IG.trigger(), IG.executeRequest(), IG.remove() and IG.submit use AJAX requests to interact with the GMSC backend. Due to the asynchronous nature of these requests, the browser does not wait for their completion, it simply continues to execute the Javascript program. The programmer has to use callbacks in order to execute code AFTER their completion.

Therefore, all of the above functions return a jQuery Promise object that facilitates the handling of AJAX request callbacks. These three methods are important for handling completed requests:

  • .done(): is called when the request has been sucessfully completed
  • .fail(): is called when the request has failed
  • .always(): is called when the request finished (no matter if it was successful or not)

In the following example the .done callback of IG.executeRequest is used to reload the form field 'LABELFEATUREGRID' after the request was successfully completed.

<FormAction name="ASSIGNLABEL" label="Apply" action="SCRIPT[IG.executeRequest({controller:'Feature', action:'SaveLabelFeatureAssignments'}).done(function(result){IG.getItemById('LABELFEATUREGRID').reload();})]"/>

Other Recipes in this category: