It is possible to participate in the lifecycle of the form. See Form Lifecycle andEvents for more details.

Fetching Additional Variables

When loading the form, the values of all variables used in the form are fetched from thebackend. This means that the form SDK only fetches those variables which are actually used in theform. The most convenient way to use a variable is the cam-variable-name directive. However,there are some situations where directive-based usage is inconvenient. In such situations it isuseful to declare additional variables programmatically:

  1. <form role="form">
  2. <div id="my-container"></div>
  3. <script cam-script type="text/form-script">
  4. var variableManager = camForm.variableManager;
  5. camForm.on('form-loaded', function() {
  6. // this callback is executed *before* the variables are loaded from the server.
  7. // if we declare a variable here, its value will be fetched as well
  8. variableManager.fetchVariable('customVariable');
  9. });
  10. camForm.on('variables-fetched', function() {
  11. // this callback is executed *after* the variables have been fetched from the server
  12. var variableValue = variableManager.variableValue('customVariable');
  13. $( '#my-container', camForm.formElement).text(variableValue);
  14. });
  15. </script>
  16. </form>

Submitting Additional Variables

Similar to fetching additional variables using a script, it is also possible to add additionalvariables to the submit:

  1. <form role="form">
  2. <script cam-script type="text/form-script">
  3. var variableManager = camForm.variableManager;
  4. camForm.on('submit', function() {
  5. // this callback is executed when the form is submitted, *before* the submit request to
  6. // the server is executed
  7. // creating a new variable will add it to the form submit
  8. variableManager.createVariable({
  9. name: 'customVariable',
  10. type: 'String',
  11. value: 'Some Value...',
  12. isDirty: true
  13. });
  14. });
  15. </script>
  16. </form>

Implementing Custom Fields

The following is a small usage example which combines some of the features explained so far.It uses custom JavaScript to implement a custom interaction with a form field which does notuse any cam-variable-* directives.

It shows how custom scripting can be used for

  • declaring a variable to be fetched from the backend,
  • writing the variable’s value to a form field,
  • reading the value upon submit.

    1. <form role="form">

    2. <!— custom control which does not use cam-variable* directives —>
      <input type="text"

    3.    class=&#34;form-control&#34;
    4.    id=&#34;customField&#34;&gt;
    5. <script cam-script type="text/form-script">
      var variableManager = camForm.variableManager;
      var customField = $('#customField', camForm.formElement);

    6. camForm.on('form-loaded', function() {

    7. // fetch the variable &#39;customVariable&#39;
    8. variableManager.fetchVariable(&#39;customVariable&#39;);
    9. });

    10. camForm.on('variables-fetched', function() {

    11. // value has been fetched from the backend
    12. var value = variableManager.variableValue(&#39;customVariable&#39;);
    13. // write the variable value to the form field
    14. customField.val(value);
    15. });

    16. camForm.on('submit', function(evt) {

    17. var fieldValue = customField.val();
    18. var backendValue = variableManager.variable(&#39;customVariable&#39;).value;
    19. if(fieldValue === backendValue) {
    20.   // prevent submit if value of form field was not changed
    21.   evt.submitPrevented = true;
    22. } else {
    23.   // set value in variable manager so that it can be sent to backend
    24.   variableManager.variableValue(&#39;customVariable&#39;, fieldValue);
    25. }
    26. });
      </script>

</form>

The above example uses jQuery for interacting with the HTML controls. If you use AngularJS, you can also populate the $scope in the variables-fetched callback and read the values from the $scope in the submit callback:

  1. <form role="form">
  2. <!-- custom control which does not use cam-variable* directives
  3. but binds to the angular scope -->
  4. <input type="text"
  5. class="form-control"
  6. id="customField"
  7. ng-model="customerId">
  8. <script cam-script type="text/form-script">
  9. var variableManager = camForm.variableManager;
  10. $scope.customerId = null;
  11. camForm.on('form-loaded', function() {
  12. // fetch the variable 'customVariable'
  13. variableManager.fetchVariable('customVariable');
  14. });
  15. camForm.on('variables-fetched', function() {
  16. // value has been fetched, bind to $scope.customerId
  17. $scope.customerId = variableManager.variable('customVariable').value;
  18. });
  19. camForm.on('submit', function(evt) {
  20. // set value in variable manager so that it can be sent to backend
  21. variableManager.variableValue('customVariable', $scope.customerId);
  22. });
  23. </script>
  24. </form>

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