Commit d1095ab3 authored by Pedro Eduardo Trujillo's avatar Pedro Eduardo Trujillo
Browse files

Unifica asignación de propiedad target

Utiliza el canal SET_PROPS de los componentes para asignar nuevos
valores a la propiedad target, como si se tratara de otra propiedad más.
Para mantener la funcionalidad, se ha sustituido también el uso de
evento con el mismo nombre por la implementación del método
_onTargetPropSet, respetando la herencia de llamadas y propagando a los
componentes que se ven afectados por el cambio de valor.

Refactoriza código de gestión de SET_PROPS e implementa nuevo método
llamado justo antes de la asignación del nuevo valor, para aquellos que
necesiten hacer acciones antes de que cambie el valor.

Reemplaza en toda la aplicación el uso de los antiguos canales
UPDATE_TARGET y CHANGE_TARGET por SET_PROPS.
parent 4df9cc73
Loading
Loading
Loading
Loading
+0 −5
Original line number Diff line number Diff line
@@ -77,11 +77,6 @@ define([
				this.associatedIds.indexOf(requesterId) !== -1);
		},

		_chkTargetAndRequester: function(response) {

			return this._chkTargetIsMine(response) && this._chkRequesterIsMe(response);
		},

		_chkPublicationIsForMe: function(res) {

			if (!res.id || res.id === this.getOwnChannel()) {
+6 −20
Original line number Diff line number Diff line
@@ -23,7 +23,6 @@ define([
					ADD_TO_QUERY: 'addToQuery',
					ADDED_TO_QUERY: 'addedToQuery',
					REFRESH: 'refresh',
					UPDATE_TARGET: 'updateTarget',
					QUERY_CHANNEL_SET: 'queryChannelSet'
				},
				filterActions: {
@@ -31,8 +30,7 @@ define([
					SERIALIZED: 'serialized',
					//REQUEST_FILTER: 'requestFilter',
					ADD_TO_QUERY: 'addToQuery',
					REFRESH: 'refresh',
					UPDATE_TARGET: 'updateTarget'
					REFRESH: 'refresh'
				}
			};

@@ -107,9 +105,6 @@ define([
			},{
				event: 'REFRESH',
				channel: this._buildChannel(queryChannel, this.actions.REFRESH)
			},{
				event: 'UPDATE_TARGET',
				channel: this._buildChannel(queryChannel, this.actions.UPDATE_TARGET)
			});
		},

@@ -123,20 +118,13 @@ define([
			this._handleFilterParams(res.data);
		},

		_subUpdateTarget: function(obj) {
			// TODO no estoy seguro de la necesidad de pisar este método, ademas puede que se repitan acciones
		_onTargetPropSet: function(changeObj) {

			this.inherited(arguments);

			this._emitEvt('UPDATE_TARGET', obj);
		},

		_updateTarget: function(obj) {
			// TODO no estoy seguro de la necesidad de pisar este método, ademas puede que se repitan acciones

			this.inherited(arguments);
			const target = changeObj.newValue;

			this._emitEvt('UPDATE_TARGET', obj);
			this._publish(this._buildChannel(this.queryChannel, 'SET_PROPS'), {target});
		},

		_onQueryChannelPropSet: function(evt) {
@@ -149,13 +137,11 @@ define([

			this._removeSubscriptions([
				this._buildChannel(oldQueryChannel, this.actions.ADDED_TO_QUERY),
				this._buildChannel(oldQueryChannel, this.actions.SERIALIZED)/*,
				this._buildChannel(oldQueryChannel, this.actions.REQUEST_FILTER)*/
				this._buildChannel(oldQueryChannel, this.actions.SERIALIZED)
			]);
			this._removePublications([
				this._buildChannel(oldQueryChannel, this.actions.ADD_TO_QUERY),
				this._buildChannel(oldQueryChannel, this.actions.REFRESH),
				this._buildChannel(oldQueryChannel, this.actions.UPDATE_TARGET)
				this._buildChannel(oldQueryChannel, this.actions.REFRESH)
			]);
		},

+44 −32
Original line number Diff line number Diff line
@@ -769,57 +769,69 @@ define([

		_subSetProps: function(req) {

			var propNames = [];
			const propNames = [];

			for (var prop in req) {
				var value = this._getUnmutableValue(req[prop]),
					oldValue = this[prop];

				if (!this._checkPropIsShareable(prop)) {
					console.error('Tried to set not settable property "%s" at module "%s"', prop, this.getChannel());
			for (const [propName, propValue] of Object.entries(req)) {
				if (!this._checkPropIsShareable(propName)) {
					console.error('Tried to set not settable property "%s" at module "%s"', propName, this.getChannel());
					continue;
				}

				if (value === oldValue) {
					console.warn('Tried to update property "%s" using same value "%s" at module "%s"', prop, value,
						this.getChannel());
				}
				propNames.push(propName);

				this[prop] = value;
				propNames.push(prop);
				const newValue = this._getUnmutableValue(propValue),
					oldValue = this[propName];

				var evtKey = this._createEvent(prop + this.propSetSuffix),
					methodName = '_on' + Utilities.capitalize(prop) + 'PropSet',
					changeObj = {
						prop: prop,
						oldValue: oldValue,
						value: value
					};

				this._emitEvt(evtKey, changeObj);
				this[methodName] && this[methodName](changeObj);
				this._propagateBeforeSetProp(propName, newValue, oldValue);
				this._setProp(propName, newValue, oldValue);
				this._propagateAfterPropSet(propName, newValue, oldValue);
			}

			this._emitEvt('PROPS_SET', {
				propNames: propNames
			});
			this._emitEvt('PROPS_SET', {propNames});
		},

		_getUnmutableValue: function(value) {

			var valueIsObject = value && typeof value === 'object';
			const valueIsObject = value && typeof value === 'object',
				objectHasLifecycle = value?.ownChannel || value?.ownerDocument || value?.constructor;

			if (!valueIsObject) {
			if (!valueIsObject || objectHasLifecycle) {
				return value;
			}

			var objectHasLifecycle = value.ownChannel || value.ownerDocument || value.constructor;
			return lang.clone(value);
		},

		_propagateBeforeSetProp: function(propName, newValue, oldValue) {

			if (objectHasLifecycle) {
				return value;
			const methodName = `_onSetProp${Utilities.capitalize(propName)}`;

			this[methodName]?.({propName, newValue, oldValue});
		},

		_setProp: function(propName, newValue, oldValue) {

			if (newValue === oldValue) {
				console.warn('Tried to update property "%s" using same value "%s" at module "%s"', propName, newValue,
					this.getChannel());
			}

			return lang.clone(value);
			this[propName] = newValue;
		},

		_propagateAfterPropSet: function(propName, newValue, oldValue) {

			const evtKey = this._createEvent(propName + this.propSetSuffix),
				methodName = `_on${Utilities.capitalize(propName)}PropSet`;

			const changeObj = {
				prop: propName, // TODO eliminar cuando se unifique el uso de los nombres de abajo
				value: newValue, // TODO eliminar cuando se unifique el uso de los nombres de abajo
				propName, newValue, oldValue
			};

			this._emitEvt(evtKey, changeObj);
			this[methodName]?.(changeObj);
		},

		_subGetProps: function(req) {
+9 −16
Original line number Diff line number Diff line
@@ -28,7 +28,6 @@ define([
			ITEM_AVAILABLE: "itemAvailable",
			INJECT_ITEM: "injectItem",
			INJECT_DATA: "injectData",
			UPDATE_TARGET: "updateTarget",
			TARGET_LOADING: "targetLoading",
			TARGET_LOADED: "targetLoaded"
		},
@@ -62,9 +61,6 @@ define([
				channel : this._buildChannel(this.storeChannel, this.actions.ITEM_AVAILABLE),
				callback: "_subItemAvailable",
				options: options
			},{
				channel : this.getChannel(this.actions.UPDATE_TARGET),
				callback: "_subUpdateTarget"
			});

			!this.omitLoading && this.subscriptionsConfig.push({
@@ -131,32 +127,29 @@ define([
			this._tryToEmitEvt('LOADED');
		},

		_subUpdateTarget: function(obj) {
		_onSetPropTarget: function() {

			this.inherited(arguments);

			this._tryToEmitEvt('LOADED');
		},

			if (obj.refresh || this.target !== obj.target) {
				this.target = obj.target;
		_chkTargetAndRequester: function(res) {

				this._updateTarget && this._updateTarget(obj);
			}
			return this._chkTargetIsMine(res) && this._chkRequesterIsMe(res);
		},

		_chkTargetLoadingIsMine: function(res) {

			if (this._shouldOmitLoadingEvents()) {
				return false;
			}

			return this._chkTargetAndRequester(res);
			return !this._shouldOmitTargetLoading(res) && this._chkTargetAndRequester(res);
		},

		_subTargetLoading: function(res) {
		_subTargetLoading: function() {

			this._tryToEmitEvt('LOADING');
		},

		_subTargetLoaded: function(res) {
		_subTargetLoaded: function() {

			this._tryToEmitEvt('LOADED');
		},
+1 −6
Original line number Diff line number Diff line
@@ -20,12 +20,7 @@ define([
				"_dataAvailable": {},
				"_itemAvailable": {},
				"_errorAvailable": {},
				"_shouldAbortRequest": {},
				"_getRequestObj": {},
				"_shouldAbortGet": {},
				"_getGetObj": {},
				"_removeData": {},
				"_shouldOmitLoadingEvents": {}
				"_shouldOmitTargetLoading": {}
			});
		}
	});
Loading