EspoCRM – Open Source CRM Application
PHP calendarcontactscrmcrm-platformcrm-systemcustomer-portalcustomer-supportcustomizabledocumentsemail-marketingkanbanleadsopen-sourcephpplatformsales-automationsingle-page-applicationsupport
list to detail collection cloning and sync
Yuri Kuznetsov80c5decee8c1
diff --git a/client/src/collection.js b/client/src/collection.jsindex 956117f982..af201b9abc 100644--- a/client/src/collection.js+++ b/client/src/collection.js@@ -57,6 +57,15 @@ import _ from 'underscore';* @param {Object} o Options.*/+/**+ * On model sync.+ *+ * @event Collection#model-sync+ * @param {Model} model A model.+ * @param {Record & {action?: 'fetch'|'save'|'destroy'}} o Options.+ * @since 9.1.0+ */+/*** A collection.*@@ -934,10 +943,17 @@ class Collection {/*** Clone.*- * @return {import('collection').default}+ * @param {{withModels?: boolean}} [options]+ * @return {Collection}*/- clone() {- const collection = new this.constructor(this.models, {+ clone(options = {}) {+ let models = this.models;++ if (options.withModels) {+ models = this.models.map(m => m.clone());+ }++ const collection = new this.constructor(models, {model: this.model,entityType: this.entityType,defs: this.defs,@@ -1057,6 +1073,7 @@ class Collection {/** @private */_onModelEvent(event, model, collection, options) {+ // @todo Revise. Never triggerred? Remove?if (event === 'sync' && collection !== this) {return;}diff --git a/client/src/model.js b/client/src/model.jsindex 51215358a5..96b0feecac 100644--- a/client/src/model.js+++ b/client/src/model.js@@ -45,7 +45,7 @@ import _ from 'underscore';* @event Model#sync* @param {Model} model A model.* @param {Object} response Response from backend.- * @param {Object.<string, *>} o Options.+ * @param {Record.<string, *> & {action?: 'fetch'|'save'|'destroy'}} o Options.*//**@@ -486,7 +486,13 @@ class Model {success.call(options.context, this, response, options);}+ options.action = 'fetch';+this.trigger('sync', this, response, options);++ if (this.collection) {+ this.collection.trigger('model-sync', this, options);+ }};this.lastSyncPromise = this.sync('read', this, options);@@ -534,7 +540,13 @@ class Model {success.call(options.context, this, response, options);}+ options.action = 'save';+this.trigger('sync', this, response, options);++ if (this.collection) {+ this.collection.trigger('model-sync', this, options);+ }};const error = options.error;@@ -570,13 +582,13 @@ class Model {/*** Delete the record in the backend.*- * @param {{wait: boolean} & Object.<string, *>} [options] Options.+ * @param {{wait?: boolean} & Object.<string, *>} [options] Options.* @returns {Promise}* @fires Model#sync* @copyright Credits to Backbone.js.*/- destroy(options) {- options = _.clone(options || {});+ destroy(options = {}) {+ options = {...options}const success = options.success;@@ -595,7 +607,15 @@ class Model {}if (!this.isNew()) {- this.trigger('sync', this, response, options);+ const syncOptions = {...options};++ syncOptions.action = 'destroy';++ this.trigger('sync', this, response, syncOptions);++ if (this.collection) {+ this.collection.trigger('model-sync', this, syncOptions);+ }}};diff --git a/client/src/multi-collection.js b/client/src/multi-collection.jsindex 08bfaac05c..ab1e5b3777 100644--- a/client/src/multi-collection.js+++ b/client/src/multi-collection.js@@ -73,8 +73,8 @@ class MultiCollection extends Collection {}/** @inheritDoc */- clone() {- const collection = super.clone();+ clone(options) {+ const collection = super.clone(options);collection.seeds = this.seeds;return collection;diff --git a/client/src/views/record/list.js b/client/src/views/record/list.jsindex e47f7e833d..07be46eeab 100644--- a/client/src/views/record/list.js+++ b/client/src/views/record/list.js@@ -2149,18 +2149,39 @@ class ListRecordView extends View {* @param {string} id*/processLinkClick(id) {- const model = this.collection.get(id);const scope = this.getModelScope(id);- const clonedModel = model.clone();+ const collection = this.collection.clone({withModels: true});const options = {id: id,- model: clonedModel,+ model: collection.get(id),};- this.listenTo(clonedModel, 'sync', () => {- model.setMultiple(clonedModel.getClonedAttributes(), {sync: true});+ this.listenTo(collection, 'model-sync', (/** Model */m, /** Record */o) => {+ const model = this.collection.get(m.id);++ if (!model) {+ return;+ }++ if (o.action === 'fetch' || o.action === 'save') {+ model.setMultiple(m.getClonedAttributes(), o);+ }+ });++ this.listenTo(collection, 'sync', (c, r, /** Record */o) => {+ if (!o.more) {+ return;+ }++ const moreModels = collection.models.slice(this.collection.length);++ this.collection.add(moreModels);+ this.collection.total = collection.total;+ this.collection.lengthCorrection = collection.lengthCorrection;++ this.collection.trigger('sync', o);});if (this.options.keepCurrentRootUrl) {