From ce10e55a46ceaa7e2bbd9cce18d1a25f5465778b Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Thu, 25 Jul 2013 15:04:48 -0700 Subject: [PATCH] CRM-12923 - Add CRM.Backbone.trackSoftDelete ---------------------------------------- * CRM-12923: Create HTML prototype of "Job Position" UI http://issues.civicrm.org/jira/browse/CRM-12923 --- js/crm.backbone.js | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/js/crm.backbone.js b/js/crm.backbone.js index fbd52f6706..9d5e2cba7e 100644 --- a/js/crm.backbone.js +++ b/js/crm.backbone.js @@ -152,6 +152,9 @@ }, _saved_onchange: function(model, options) { if (options.parse) return; + this.setModified(); + }, + setModified: function() { var oldModified = this._modified; this._modified = true; if (!oldModified) { @@ -185,6 +188,48 @@ }; /** + * Configure a model class to support client-side soft deletion. + * One can call "model.setDeleted(BOOLEAN)" to flag an entity for + * deletion (or not) -- however, deletion will be deferred until save() + * is called. + * + * Events: + * softDelete: function(model, is_deleted) -- change value of is_deleted + * + * @param ModelClass + */ + CRM.Backbone.trackSoftDelete = function(ModelClass) { + // Retain references to some of the original class's functions + var Parent = _.pick(ModelClass.prototype, 'save'); + + // Defaults - if specified in ModelClass, preserve + _.defaults(ModelClass.prototype, { + is_soft_deleted: false, + setSoftDeleted: function(is_deleted) { + if (this.is_soft_deleted != is_deleted) { + this.is_soft_deleted = is_deleted; + this.trigger('softDelete', this, is_deleted); + if (this.setModified) this.setModified(); // FIXME: ugly interaction, trackSoftDelete-trackSaved + } + }, + isSoftDeleted: function() { + return this.is_soft_deleted; + } + }); + + // Overrides - if specified in ModelClass, replace + _.extend(ModelClass.prototype, { + save: function(attributes, options) { + if (this.isSoftDeleted()) { + return this.destroy(options); + } else { + return Parent.save.apply(this, arguments); + } + } + }); + }; + + /** * Connect a "collection" class to CiviCRM's APIv3 * * Note: the collection supports a special property, crmCriteria, which is an array of -- 2.25.1