The following examples show example scenarios of custom JavaScript in embedded forms.

User name from a cam-script

This example demonstrates how to retrieve the user name and display it in an embedded form:

  1. <form role="form">
  2. <script cam-script type="text/form-script">
  3. inject(['$rootScope', function($rootScope) {
  4. $scope.userName = $rootScope.authentication.name;
  5. }]);
  6. </script>
  7. <h1>Greetings {{ userName }}</h1>
  8. </form>

Load Additional Resources

This example includes an image, which is located in the contextPath of the form (i.e., in the same directory). The URL of the image is retrieved via the task form key method of the REST API:

  1. <form role="form">
  2. <script cam-script type="text/form-script">
  3. inject(['$http', 'Uri', function($http, Uri) {
  4. camForm.on('form-loaded', function() {
  5. $http.get(Uri.appUri("engine://engine/:engine/task/" + camForm.taskId + "/form")).success(function(result){
  6. $scope.contextPath = result.contextPath;
  7. });
  8. });
  9. }]);
  10. </script>
  11. <img ng-src="{{contextPath}}/image.png" />
  12. </form>

Exclude a variable from submit

When a variable is loaded, it is also sent back to the server when the form is submitted. If you have a variable that you don’t want to be submitted when the form is completed, you can use the destroyVariable function of the variable manager:

  1. <script cam-script type="text/form-script">
  2. camForm.on('form-loaded', function() {
  3. // tell the form SDK to fetch the variable named 'invoiceData'
  4. camForm.variableManager.fetchVariable('invoiceData');
  5. });
  6. camForm.on('variables-fetched', function() {
  7. // work with the variable (bind it to the current AngularJS $scope)
  8. $scope.invoiceData = camForm.variableManager.variableValue('invoiceData');
  9. });
  10. camForm.on('submit', function() {
  11. // make the variableManager forget about the invoiceData variable
  12. camForm.variableManager.destroyVariable('invoiceData');
  13. });
  14. </script>

Upload Large Files

This example contains a file input element and the script to send it to the server. In contrast to the file input element of the Forms SDK, this example can handle large files, but it also has some drawbacks:

  • Can not be used in the start form of a process (no process instance id exists at this time)
  • Does not take part in the form lifecycle (files could be saved even if the form is not submitted)
  • Can only save one file at a time
    This example first retrieves the process instance id of the task for the form. It then registers an upload function, which, when executed, uploads the data as a process instance variable with the name uploadedFile
  1. <form role="form">
  2. <input id="fileUpload"
  3. type="file" />
  4. <button ng-click="upload()">Upload</button>
  5. <script cam-script type="text/form-script">
  6. inject(['$http', 'Uri', function($http, Uri) {
  7. camForm.on('form-loaded', function() {
  8. $http.get(Uri.appUri('engine://engine/:engine/task/' + camForm.taskId)).success(function(result){
  9. $scope.upload = function() {
  10. var formData = new FormData();
  11. formData.append('data', document.getElementById('fileUpload').files[0]);
  12. $http.post(Uri.appUri('engine://engine/:engine/process-instance/' + result.processInstanceId + '/variables/uploadedFile/data'), formData, {
  13. transformRequest: angular.identity,
  14. headers: {'Content-Type': undefined}
  15. });
  16. };
  17. });
  18. });
  19. }]);
  20. </script>
  21. </form>

原文: https://docs.camunda.org/manual/7.9/reference/embedded-forms/javascript/examples/