Merge pull request #4757 from davecivicrm/CRM-15409
[civicrm-core.git] / js / crm.backbone.js
index 87c1c5cbc3f0aa0df8e8ce680c570aa72e0522ea..eb45249c1717e9fd3ac194efdfc7d0a811baabb0 100644 (file)
@@ -1,5 +1,4 @@
-(function($) {
-  var CRM = (window.CRM) ? (window.CRM) : (window.CRM = {});
+(function($, _) {
   if (!CRM.Backbone) CRM.Backbone = {};
 
   /**
       };
       switch (method) {
         case 'read':
-          CRM.api(model.crmEntityName, 'get', model.toCrmCriteria(), apiOptions);
+          CRM.api(model.crmEntityName, model.toCrmAction('get'), model.toCrmCriteria(), apiOptions);
           break;
         // replace all entities matching "x.crmCriteria" with new entities in "x.models"
         case 'crm-replace':
           var params = this.toCrmCriteria();
           params.version = 3;
           params.values = this.toJSON();
-          CRM.api(model.crmEntityName, 'replace', params, apiOptions);
+          CRM.api(model.crmEntityName, model.toCrmAction('replace'), params, apiOptions);
           break;
         default:
           apiOptions.error({is_error: 1, error_message: "CRM.Backbone.sync(" + method + ") not implemented for collections"});
@@ -74,9 +73,9 @@
           params.options || (params.options = {});
           params.options.reload = 1;
           if (!model._isDuplicate) {
-            CRM.api(model.crmEntityName, 'create', params, apiOptions);
+            CRM.api(model.crmEntityName, model.toCrmAction('create'), params, apiOptions);
           } else {
-            CRM.api(model.crmEntityName, 'duplicate', params, apiOptions);
+            CRM.api(model.crmEntityName, model.toCrmAction('duplicate'), params, apiOptions);
           }
           break;
         case 'read':
@@ -87,7 +86,7 @@
             apiOptions.error({is_error: 1, error_message: 'Missing ID for ' + model.crmEntityName});
             return;
           }
-          CRM.api(model.crmEntityName, apiAction, params, apiOptions);
+          CRM.api(model.crmEntityName, model.toCrmAction(apiAction), params, apiOptions);
           break;
         default:
           apiOptions.error({is_error: 1, error_message: "CRM.Backbone.sync(" + method + ") not implemented for models"});
     // Defaults - if specified in ModelClass, preserve
     _.defaults(ModelClass.prototype, {
       crmEntityName: crmEntityName,
+      crmActions: {}, // map: string backboneActionName => string serverSideActionName
+      crmReturn: null, // array: list of fields to return
+      toCrmAction: function(action) {
+        return this.crmActions[action] ? this.crmActions[action] : action;
+      },
       toCrmCriteria: function() {
-        return (this.get('id')) ? {id: this.get('id')} : {};
+        var result = (this.get('id')) ? {id: this.get('id')} : {};
+        if (this.crmReturn != null) {
+          result.return = this.crmReturn;
+        }
+        return result;
       },
       duplicate: function() {
         var newModel = new ModelClass(this.toJSON());
     // Defaults - if specified in CollectionClass, preserve
     _.defaults(CollectionClass.prototype, {
       crmEntityName: CollectionClass.prototype.model.prototype.crmEntityName,
+      crmActions: {}, // map: string backboneActionName => string serverSideActionName
+      toCrmAction: function(action) {
+        return this.crmActions[action] ? this.crmActions[action] : action;
+      },
       toCrmCriteria: function() {
-        return (this.crmCriteria) ? _.extend({}, this.crmCriteria) : {};
+        var result = (this.crmCriteria) ? _.extend({}, this.crmCriteria) : {};
+        if (this.crmReturn != null) {
+          result.return = this.crmReturn;
+        } else if (this.model && this.model.prototype.crmReturn != null) {
+          result.return = this.model.prototype.crmReturn;
+        }
+        return result;
       },
 
       /**
 
       debouncedFetch: _.debounce(function() {
         this.fetch({reset: true});
-      }, 500),
+      }, 100),
 
       /**
        * Reconcile the server's collection with the client's collection.
         } else if (options.crmCriteria) {
           this.crmCriteria = options.crmCriteria;
         }
+        if (options.crmActions) {
+          this.crmActions = _.extend(this.crmActions, options.crmActions);
+        }
         if (origInit) {
           return origInit.apply(this, arguments);
         }
   var wrapError = function (model, options) {
     var error = options.error;
     options.error = function(resp) {
-      if (error) error(model, resp, options);
+      if (error) error(model, resp, optio)
       model.trigger('error', model, resp, options);
     };
   };
-})(cj);
+})(CRM.$, CRM._);