CRM-12911 - CRM.Backbone.sync - Add mechanism for API-assisted duplication of entities
authorTim Otten <totten@civicrm.org>
Fri, 2 Aug 2013 22:41:33 +0000 (15:41 -0700)
committerTim Otten <totten@civicrm.org>
Fri, 2 Aug 2013 23:24:43 +0000 (16:24 -0700)
----------------------------------------
* CRM-12911: Implement "Duplicate" feature for Jobs
  http://issues.civicrm.org/jira/browse/CRM-12911

js/crm.backbone.js

index f159c9e053132ddf2e3c93221c2b1b755877d3ef..6d02cc246f95be4ab59c1099c51ed4620db6b802 100644 (file)
       switch (method) {
         case 'create': // pass-through
         case 'update':
-          CRM.api(model.crmEntityName, 'create', model.toJSON(), apiOptions);
+          if (!model._isDuplicate) {
+            CRM.api(model.crmEntityName, 'create', model.toJSON(), apiOptions);
+          } else {
+            CRM.api(model.crmEntityName, 'duplicate', model.toJSON(), apiOptions);
+          }
           break;
         case 'read':
         case 'delete':
       crmEntityName: crmEntityName,
       toCrmCriteria: function() {
         return (this.get('id')) ? {id: this.get('id')} : {};
+      },
+      duplicate: function() {
+        var newModel = new ModelClass(this.toJSON());
+        newModel._isDuplicate = true;
+        if (newModel.setModified) newModel.setModified();
+        newModel.listenTo(newModel, 'sync', function(){
+          // may get called on subsequent resaves -- don't care!
+          delete newModel._isDuplicate;
+        });
+        return newModel;
       }
     });
     // Overrides - if specified in ModelClass, replace