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 directive. However,there are some situations where directive-based usage is inconvenient. In such situations it isuseful to declare additional variables programmatically:

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

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

    6. // fetch the variable &#39;customVariable&#39;
    7. });

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

    9. var value = variableManager.variableValue(&#39;customVariable&#39;);
    10. // write the variable value to the form field
    11. customField.val(value);
    12. camForm.on('submit', function(evt) {

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

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>