Merge pull request #165 from colemanw/CRM-12122
[civicrm-core.git] / js / rest.js
index 1e07f11105a84b8db74aad71ff67de1be31e05c9..0cdb94f71c931a6f3c959f480264ffc0ef7da7af 100644 (file)
@@ -86,7 +86,9 @@ var CRM = CRM || {};
    */
   CRM.api = function(entity, action, params, options) {
     // Default settings
-    var settings = {
+    var json = false,
+    settings = {
+      context: null,
       success: function(result, settings) {
         return true;
       },
@@ -120,8 +122,6 @@ 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;
@@ -148,26 +148,26 @@ 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);
   };
 
   $.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);