Skip to content

EspoCRM – Open Source CRM Application

event wrappers

Yurii97af3c2bbb1f
diff --git a/client/src/util/event.js b/client/src/util/event.js
new file mode 100644
index 0000000000..2145fe2762
--- /dev/null
+++ b/client/src/util/event.js
@@ -0,0 +1,137 @@
+/************************************************************************
+ * This file is part of EspoCRM.
+ *
+ * EspoCRM – Open Source CRM application.
+ * Copyright (C) 2014-2026 EspoCRM, Inc.
+ * Website: https://www.espocrm.com
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ *
+ * The interactive user interfaces in modified source and object code versions
+ * of this program must display Appropriate Legal Notices, as required under
+ * Section 5 of the GNU Affero General Public License version 3.
+ *
+ * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
+ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
+ ************************************************************************/
+
+/**
+ * Listen to model attribute change.
+ *
+ * @param {{
+ * owner: import('view').default | import('model').default | import('collection').default,
+ * target: import('model').default,
+ * attributes?: string[],
+ * once?: boolean,
+ * callback: function({
+ * ui: boolean|null,
+ * action: string|'ui'|'save'|'fetch'|'cancel-edit'|null,
+ * }),
+ * }} params
+ * @return {{stop: function()}}
+ * @since 9.4.0
+ */
+export function onModelChange(params) {
+ const owner = params.owner;
+ const target = params.target;
+ const callback = params.callback;
+
+ let event = 'change';
+
+ if (params.attributes) {
+ event = params.attributes
+ .map(it => `change:${it}`)
+ .join(' ');
+ }
+
+ /**
+ * @param {Record} o
+ */
+ function callCallback(o) {
+ callback({
+ ui: o.ui ?? null,
+ action: o.action ?? null,
+ });
+ }
+
+ const wrappedCallback = params.attributes ?
+ (m, v, o) => callCallback(o) :
+ (m, o) => callCallback(o);
+
+ function stop() {
+ owner.stopListening(target, event, wrappedCallback);
+ }
+
+ const output = {stop};
+
+ if (params.once) {
+ owner.listenToOnce(target, event, wrappedCallback);
+
+ return output;
+ }
+
+ owner.listenTo(target, event, wrappedCallback);
+
+ return output;
+}
+
+/**
+ * Listen to sync.
+ *
+ * @param {{
+ * owner: import('view').default | import('model').default | import('collection').default,
+ * target: import('model').default | import('collection').default,
+ * once?: boolean,
+ * callback: function({
+ * action: 'fetch'|'save'|'destroy'|null,
+ * response: *,
+ * }),
+ * }} params
+ * @return {{stop: function()}}
+ * @since 9.4.0
+ */
+export function onSync(params) {
+ const owner = params.owner;
+ const target = params.target;
+ const callback = params.callback;
+
+ const event = 'sync';
+
+ /**
+ * @param {Record} o
+ * @param {*} response
+ */
+ function wrappedCallback(o, response) {
+ callback({
+ action: o.action ?? null,
+ response: response,
+ });
+ }
+
+ function stop() {
+ owner.stopListening(target, event, wrappedCallback);
+ }
+
+ const output = {stop};
+
+ if (params.once) {
+ owner.listenToOnce(target, event, wrappedCallback);
+
+ return output;
+ }
+
+ owner.listenTo(target, event, wrappedCallback);
+
+ return output;
+}
diff --git a/client/src/views/fields/wysiwyg.js b/client/src/views/fields/wysiwyg.js
index 95c29ab298..f4b3bb318f 100644
--- a/client/src/views/fields/wysiwyg.js
+++ b/client/src/views/fields/wysiwyg.js
@@ -30,6 +30,7 @@
import TextFieldView from 'views/fields/text';
import {init as initSummernoteCustom} from 'helpers/misc/summernote-custom';
+import {onModelChange} from 'util/event';
/**
* A wysiwyg field.
@@ -158,41 +159,46 @@ class WysiwygFieldView extends TextFieldView {
return;
}
- this.listenTo(this.model, 'change:isHtml', (model, value, o) => {
- if (o.ui && this.isEditMode()) {
- if (!this.isRendered()) {
- return;
- }
+ onModelChange({
+ owner: this,
+ target: this.model,
+ attributes: ['isHtml'],
+ callback: o => {
+ if (o.ui && this.isEditMode()) {
+ if (!this.isRendered()) {
+ return;
+ }
- if (this.isHtml()) {
- let value = this.plainToHtml(this.model.get(this.name));
+ if (this.isHtml()) {
+ let value = this.plainToHtml(this.model.get(this.name));
- if (
- this.lastHtmlValue &&
- this.model.get(this.name) === this.htmlToPlain(this.lastHtmlValue)
- ) {
- value = this.lastHtmlValue;
- }
+ if (
+ this.lastHtmlValue &&
+ this.model.get(this.name) === this.htmlToPlain(this.lastHtmlValue)
+ ) {
+ value = this.lastHtmlValue;
+ }
- this.model.set(this.name, value, {skipReRender: true});
- this.enableWysiwygMode();
+ this.model.set(this.name, value, {skipReRender: true});
+ this.enableWysiwygMode();
- return;
- }
+ return;
+ }
- this.lastHtmlValue = this.model.get(this.name);
- const value = this.htmlToPlain(this.model.get(this.name));
+ this.lastHtmlValue = this.model.get(this.name);
+ const value = this.htmlToPlain(this.model.get(this.name));
- this.disableWysiwygMode();
- this.model.set(this.name, value);
+ this.disableWysiwygMode();
+ this.model.set(this.name, value);
- return;
- }
+ return;
+ }
- if (this.isDetailMode() && this.isRendered()) {
- this.reRender();
- }
- });
+ if (this.isDetailMode() && this.isRendered()) {
+ this.reRender();
+ }
+ },
+ })
}
data() {
diff --git a/client/src/views/record/kanban.js b/client/src/views/record/kanban.js
index 988c14f021..e5c731c0f7 100644
--- a/client/src/views/record/kanban.js
+++ b/client/src/views/record/kanban.js
@@ -30,6 +30,7 @@
import ListRecordView from 'views/record/list';
import RecordModal from 'helpers/record-modal';
+import {onSync} from 'util/event';
/**
* A kanban record view.
@@ -327,11 +328,14 @@ class KanbanRecordView extends ListRecordView {
throw new Error('Collection has not been injected into Record.List view.');
}
- this.listenTo(this.collection, 'sync', (c, response) => {
- this.subCollectionList = undefined;
-
- // noinspection JSUnresolvedReference
- this.groupRawDataList = response.groups;
+ onSync({
+ owner: this,
+ target: this.collection,
+ callback: o => {
+ this.subCollectionList = undefined;
+ // noinspection JSUnresolvedReference
+ this.groupRawDataList = o.response.groups;
+ },
});
this.layoutLoadCallbackList = [];