title: angularjs: extending directive then make modifications
body: <p>I am working in ServiceNow and am trying to extend an ootb directive, then make some modifications to a couple functions.  So far I've figured out how to extend the directive:</p>
  
  <pre><code>function (spModelDirective){
      return angular.extend({}, spModelDirective[0], {
          templateUrl:'lbl_custom_template.xml'
      });
  }
  </code></pre>
  
  <p>Within this directive, there's a function called getNestedFields that I would like to make edits to:</p>
  
  <pre><code>function getNestedFields(fields, containers) {
      if (!containers)
          return;
      for (var _container in containers) {
          var container = containers[_container];
          if (container.columns) {
              for (var _col in container.columns) {
                  var col = container.columns[_col];
                  for (var _field in col.fields) {
                      var field = col.fields[_field];
                      if (field.type == "container" &amp;&amp; container.caption != "")
                          getNestedFields(fields, [field]);
                      else if (field.type == "checkbox_container")
                          getNestedFields(fields, field.containers);
                      else if (field.type == "field" || container.caption=="")
                          fields.push(formModel._fields[field.name]);
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>Can someone provide some guidance on what the correct syntax for this would be?</p>
  
  <p><strong>More information</strong></p>
  
  <p>Our team cloned the ootb widget-form and am trying to create a custom layout.  Basically, we want each form section to be it's own tab much like the back-end form instead of one long form, which is what the ootb widget-form currently does.  In the very first line of the sp-variable-layout template, it shows:</p>
  
  <pre><code>&lt;fieldset ng-init="$last ? execItemScripts() : null" ng-show="isContainerVisible(container)" ng-repeat="container in containers"&gt;
  </code></pre>
  
  <p>The ng-repeat of container in containers considers each form section as a separate container (which is perfect), BUT it also considers any splits as a separate container as well.  So for example, if my form's layout looks like this:</p>
  
  <p><a href="https://i.stack.imgur.com/V03kX.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/V03kX.png" alt="enter image description here"></a></p>
  
  <p>This will create two tabs:  one that has every field within the begin and end splits AND a separate tab with everything after the end split.  The JSON object that is created looks like this:</p>
  
  <pre><code>{
          "_bootstrap_cells": 6,
          "_count": 2,
          "visible": true,
          "columns": [{
              "fields": [{
                  "name": "type_of_account",
                  "type": "field"
              }, {
                  "name": "routing_transit_number",
                  "type": "field"
              }]
          }, {
              "fields": [{
                  "name": "type_of_payment",
                  "type": "field"
              }, {
                  "name": "check_digit",
                  "type": "field"
              }]
          }],
          "caption": "Direct Deposit",
          "id": "b456b9d2137ac340177c36328144b0ef",
          "scope_name": "x_dnf_table"
      }, {
          "_bootstrap_cells": 12,
          "_count": 1,
          "visible": true,
          "columns": [{
              "fields": [{
                  "name": "account_number",
                  "type": "field"
              }, {
                  "name": "account_title",
                  "type": "field"
              }, {
                  "name": "financial_institution_name",
                  "type": "field"
              }]
          }],
          "caption": "",
          "id": "",
          "scope_name": "x_dnf_table"
      }
  </code></pre>
  
  <p>Notice the first "section" has a caption, but ServiceNow treats the split section as its own separate section with no caption at all.</p>
  
  <p>I want to change the spModel directive to produce only containers with captions as their own tab and if a container does NOT have a caption, to append it to the previous container that does have a caption.</p>