-- Indentation fixes
[civicrm-core.git] / js / crm.backbone.js
index 01ec9e847ab8ef1c81fd7714ad38c12354a6cb35..746ddaf7be3559ee8aa0d9529885de9a8b85d71a 100644 (file)
       switch (method) {
         case 'create': // pass-through
         case 'update':
-          CRM.api(model.crmEntityName, 'create', model.toJSON(), apiOptions);
+          var params = model.toJSON();
+          params.options || (params.options = {});
+          params.options.reload = 1;
+          if (!model._isDuplicate) {
+            CRM.api(model.crmEntityName, 'create', params, apiOptions);
+          } else {
+            CRM.api(model.crmEntityName, 'duplicate', params, 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
     // Defaults - if specified in ModelClass, preserve
     _.defaults(ModelClass.prototype, {
       isSaved: function() {
-        var result = !this.isNew() && !this._modified;
+        var result = !this.isNew() && !this.isModified();
         return result;
       },
+      isModified: function() {
+        return this._modified;
+      },
       _saved_onchange: function(model, options) {
         if (options.parse) return;
+        // console.log('change', model.changedAttributes(), model.previousAttributes());
         this.setModified();
       },
       setModified: function() {