crmMetadata - Provide both options and optionsMap
authorTim Otten <totten@civicrm.org>
Fri, 27 Feb 2015 01:08:39 +0000 (18:08 -0700)
committerTim Otten <totten@civicrm.org>
Mon, 2 Mar 2015 23:59:43 +0000 (15:59 -0800)
js/angular-crm-util.js

index 02708afe1bd374d667c2337f46f62b18dbbba2b4..3a1a8fd54ff177827d12d023028c6dbe7c0deb58 100644 (file)
   //     console.log('The fields are:', options);
   //   });
   angular.module('crmUtil').factory('crmMetadata', function($q, crmApi) {
+
+    // Convert {key:$,value:$} sequence to unordered {$key: $value} map.
+    function convertOptionsToMap(options) {
+      var result = {};
+      angular.forEach(options, function(o) {
+        result[o.key] = o.value;
+      });
+      return result;
+    }
+
     var cache = {}; // cache[entityName+'::'+action][fieldName].title
     var deferreds = {}; // deferreds[cacheKey].push($q.defer())
     var crmMetadata = {
             // on success:
             function(fields) {
               cache[cacheKey] = _.indexBy(fields.values, 'name');
+              angular.forEach(cache[cacheKey],function (field){
+                if (field.options) {
+                  field.optionsMap = convertOptionsToMap(field.options);
+                }
+              });
               angular.forEach(deferreds[cacheKey], function(dfr) {
                 dfr.resolve(cache[cacheKey]);
               });