-- Indentation fixes
[civicrm-core.git] / js / rest.js
index 1e07f11105a84b8db74aad71ff67de1be31e05c9..434dcdf312ef63172d04cb2bc6f200f052c9c80f 100644 (file)
@@ -1,6 +1,6 @@
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.3                                                |
+ | CiviCRM version 4.4                                                |
  +--------------------------------------------------------------------+
  | This file is a part of CiviCRM.                                    |
  |                                                                    |
@@ -87,6 +87,7 @@ var CRM = CRM || {};
   CRM.api = function(entity, action, params, options) {
     // Default settings
     var settings = {
+      context: null,
       success: function(result, settings) {
         return true;
       },
@@ -100,7 +101,7 @@ var CRM = CRM || {};
         }
         return settings.success.call(this, result, settings);
       },
-      ajaxURL: 'civicrm/ajax/rest',
+      ajaxURL: 'civicrm/ajax/rest'
     };
     action = action.toLowerCase();
     // Default success handler
@@ -120,26 +121,11 @@ var CRM = CRM || {};
           return true;
         };
     }
-    $.extend(settings, options);
-    var json = false;
-    for (var i in params) {
-      if (i.slice(0, 4) == 'api.' || typeof(params[i]) == 'Object') {
-        json = true;
-        break;
-      }
-    }
-    if (json) {
-      params = {
-        entity: entity,
-        action: action,
-        json: JSON.stringify(params)
-      };
-    }
-    else {
-      params.entity = entity;
-      params.action = action;
-      params.json = 1;
-    }
+    params = {
+      entity: entity,
+      action: action,
+      json: JSON.stringify(params)
+    };
     // Pass copy of settings into closure to preserve its value during multiple requests
     (function(stg) {
       $.ajax({
@@ -148,26 +134,35 @@ var CRM = CRM || {};
         data: params,
         type: action.indexOf('get') < 0 ? 'POST' : 'GET',
         success: function(result) {
-          stg.callBack.call(this, result, stg);
+          stg.callBack.call(stg.context, result, stg);
         }
       });
-    })($.extend({}, settings));
+    })($.extend({}, settings, options));
   };
 
   // Backwards compatible with jQuery fn
   $.fn.crmAPI = function(entity, action, params, options) {
     console && console.log && console.log('Calling crmAPI from jQuery is deprecated. Please use CRM.api() instead.');
-    return CRM.api(entity, action, params, options);
+    return CRM.api.call(this, entity, action, params, options);
   };
 
+  /**
+   * FIXME: This function is not documented, is not used anywhere in the codebase, and doesn't
+   * clearly differentiate field elements which store the "contact id" versus the "contact label".
+   * My guess is that it's designed more for "quick-search" and less for "CRUD forms".
+   *
+   * @param params
+   * @param options
+   * @return {*}
+   */
   $.fn.crmAutocomplete = function (params, options) {
     if (typeof params == 'undefined') params = {};
     if (typeof options == 'undefined') options = {};
-    params = $().extend( {
+    params = $().extend({
       rowCount:35,
       json:1,
       entity:'Contact',
-        action:'getquick',
+      action:'getquick',
       sequential:1
     }, params);