Implement Address block with chainSelect state/county
[civicrm-core.git] / ext / afform / core / ang / af / Field.js
index 5bf7c9b8db6782ad0554dcc8a107ae87d4090277..554eb8221804ac8c4b533184ead2546ff7aa3464 100644 (file)
@@ -1,7 +1,7 @@
 (function(angular, $, _) {
   var id = 0;
   // Example usage: <div af-fieldset="myModel"><af-field name="do_not_email" /></div>
-  angular.module('af').directive('afField', function() {
+  angular.module('af').directive('afField', function(crmApi4) {
     return {
       restrict: 'E',
       require: ['^^afForm', '^^afFieldset', '?^^afJoin', '?^^afRepeatItem'],
             }, [])
           };
         };
+
+        // ChainSelect - watch control field & reload options as needed
+        if ($scope.defn.input_type === 'ChainSelect') {
+          $scope.$watch('dataProvider.getFieldData()[defn.input_attrs.controlField]', function(val) {
+            if (val) {
+              var params = {
+                where: [['name', '=', $scope.fieldName]],
+                select: ['options'],
+                loadOptions: true,
+                values: {}
+              };
+              params.values[$scope.defn.input_attrs.controlField] = val;
+              crmApi4($scope.dataProvider.getEntityType(), 'getFields', params, 0)
+                .then(function(data) {
+                  $scope.defn.options.length = 0;
+                  _.transform(data.options, function(options, label, key) {
+                    options.push({key: key, label: label});
+                  }, $scope.defn.options);
+                });
+            }
+          });
+        }
       }
     };
   });