Commit 0c2cbb98 authored by Pedro Eduardo Trujillo's avatar Pedro Eduardo Trujillo
Browse files

Elimina persistencia aberrante aplicada a modelos

Los widgets de modelos (no el módulo, sino la definición que instancia
este) contenían una funcionalidad de persistencia que se aparta del
resto de persistencia en la app. Por si fuera poco, se invocaba dicha
funcionalidad con llamadas a método directas, y desde módulos
independientes (para colmo).
Ahora, en lugar de pegar funcionalidades de mala manera y usarlas sin
criterio alguno, el módulo de modelos hace uso de la base de
persistencia común, y recibe la orden de guardar datos a través de una
publicación, no llamando directamente a ningún método.
De esta manera, también se hacen las peticiones de guardado a través de
RestManager, eliminando la dependencia de dojo/request.
parent 0334e323
Loading
Loading
Loading
Loading
+0 −76
Original line number Diff line number Diff line
define([
	'app/redmicConfig'
	, 'dojo/_base/declare'
	, 'dojo/_base/lang'
	, 'dojo/Deferred'
	, 'dojo/json'
	, 'dojo/request'
], function(
	redmicConfig
	, declare
	, lang
	, Deferred
	, JSON
	, request
){
	return declare(null, {
		//	summary:
		//		Funcionalidades de persistencia para el modelo.
		//	description:
		//		Proporciona método 'save' al modelo.

		constructor: function(args) {

			this.config = {
				headers: {
					'Content-Type': 'application/json',
					'Accept': 'application/javascript, application/json'
				},
				handleAs: 'json'
			};

			lang.mixin(this, this.config, args);
		},

		save: function() {

			if (!this.isValid) {
				console.error('Tried to save invalid model \'%s\' with this schema:', this.get('modelName'),
					this.get('schema'));

				return;
			}

			if (!this.hasChanged) {
				console.error('Tried to save unchanged model \'%s\' with this schema:', this.get('modelName'),
					this.get('schema'));

				return;
			}

			var envDfd = window.env,
				dfd = new Deferred();

			if (envDfd) {
				envDfd.then(lang.hitch(this, function(retDfd, envData) {

					var id = this.getIdValue(),
						method = this.get('isNew') ? 'POST' : 'PUT',
						data = JSON.stringify(this.serialize()),
						target = redmicConfig.getServiceUrl(this.target, envData) + '/';

					var reqDfd = request(id ? target + id : target, {
						headers: this.headers,
						handleAs: this.handleAs,
						method: method,
						data: data
					});

					reqDfd.then(lang.hitch(retDfd, retDfd.resolve), lang.hitch(retDfd, retDfd.reject));
				}, dfd));
			}

			return dfd;
		}
	});
});
+14 −19
Original line number Diff line number Diff line
@@ -36,11 +36,7 @@ define([

			this.activityModelInstance = new ModelImpl({
				parentChannel: this.getChannel(),
				persistent: true,
				target: redmicConfig.services.activity,
				props: {
				target: redmicConfig.services.activity
				}
			});
		},

@@ -154,9 +150,17 @@ define([

		_wasValidActivityModel: function(res) {

			this.activityModelInstance.modelInstance.save().then(
				lang.hitch(this, this._handleResponse),
				lang.hitch(this, this._handleError));
			this._once(this.activityModelInstance.getChannel('SAVED'), lang.hitch(this, this._afterModelSave));
			this._publish(this.activityModelInstance.getChannel('SAVE'), {});
		},

		_afterModelSave: function(res) {

			if (res.success) {
				this._handleResponse(res.data);
			} else {
				this._handleError(res.data);
			}
		},

		_handleResponse: function(result) {
@@ -168,11 +172,7 @@ define([
			//		callback private
			//

			if (!result.success) {
				this._handleError(result.error);
			} else if (result && result.body) {
				this._setDataActivityClosed(result.body);
			}
			this._setDataActivityClosed(result && result.body);
		},

		_handleError: function(error) {
@@ -182,11 +182,6 @@ define([
			//	tags:
			//		callback private

			// TODO: cambiar cuando esten unificados los errores de la api
			if (error.response && error.response.data) {
				error = error.response.data.error;
			}

			var msg = error.description;

			this._emitEvt('COMMUNICATION', {type: "alert", level: "error", description: msg});
+23 −31
Original line number Diff line number Diff line
@@ -27,6 +27,7 @@ define([
	, _ListenModelHasChanged
	, ModelImpl
) {

	return declare(_ExternalUserBaseView, {
		// summary:
		// 	Vista de feedback
@@ -63,11 +64,8 @@ define([

			this.modelInstance = new ModelImpl({
				parentChannel: this.getChannel(),
				persistent: true,
				schema: feedbackModelSchema,
				props: {
				target: this.target
				}
			});
		},

@@ -191,15 +189,22 @@ define([
		_wasValid: function(res) {

			if (this._submitActive && this.reCaptchaVerify && res.isValid) {
				this._emitEvt('LOADING');
				this.modelInstance.modelInstance.save().then(
					lang.hitch(this, this._handleResponse),
					lang.hitch(this, this._handleError));
				this._once(this.modelInstance.getChannel('SAVED'), lang.hitch(this, this._afterModelSave));
				this._publish(this.modelInstance.getChannel('SAVE'), {});
			}

			this._submitActive = false;
		},

		_afterModelSave: function(res) {

			if (res.success) {
				this._handleResponse(res.data);
			} else {
				this._handleError(res.data);
			}
		},

		_handleResponse: function(result) {
			//	summary:
			//		Función que maneja la respuesta,
@@ -209,9 +214,6 @@ define([
			//		callback private
			//

			this._emitEvt('LOADED');

			if (result.success) {
			alertify.alert(
				this.i18n.success,
				this.i18n.sendFeedback,
@@ -220,9 +222,6 @@ define([
					window.location.href = "/";
				})
			);
			} else {
				this._handleError(result.error);
			}
		},

		_handleError: function(error) {
@@ -232,15 +231,8 @@ define([
			//	tags:
			//		callback private

			this._emitEvt('LOADED');

			this._resetForm();

			// TODO: cambiar cuando esten unificados los errores de la api
			if (error.response && error.response.data) {
				error = error.response.data.error;
			}

			var msg = error.description;
			this._emitEvt('TRACK', {
				type: TRACK.type.exception,
+18 −28
Original line number Diff line number Diff line
@@ -72,11 +72,8 @@ define([

			this.modelInstance = new ModelImpl({
				parentChannel: this.getChannel(),
				persistent: true,
				schema: registerModelSchema,
				props: {
				target: this.target
				}
			});
		},

@@ -205,8 +202,7 @@ define([
			//	tags:
			//		callback private
			//
			this._once(this.modelInstance.getChannel("WAS_VALID"),
				lang.hitch(this, this._wasValid));
			this._once(this.modelInstance.getChannel("WAS_VALID"), lang.hitch(this, this._wasValid));

			this._submitActive = true;

@@ -216,15 +212,22 @@ define([
		_wasValid: function(res) {

			if (this._submitActive && this.reCaptchaVerify && res.isValid && this._acceptTermsAndConditions) {
				this._emitEvt('LOADING');
				this.modelInstance.modelInstance.save().then(
					lang.hitch(this, this._handleResponse),
					lang.hitch(this, this._handleError));
				this._once(this.modelInstance.getChannel('SAVED'), lang.hitch(this, this._afterModelSave));
				this._publish(this.modelInstance.getChannel('SAVE'), {});
			}

			this._submitActive = false;
		},

		_afterModelSave: function(res) {

			if (res.success) {
				this._handleResponse(res.data);
			} else {
				this._handleError(res.data);
			}
		},

		_handleResponse: function(result) {
			//	summary:
			//		Función que maneja la respuesta del registro,
@@ -234,17 +237,11 @@ define([
			//		callback private
			//

			this._emitEvt('LOADED');
			alertify.alert(this.i18n.success, this.i18n.activateAccount, lang.hitch(this, function() {

			if (result.success) {
				alertify.alert(this.i18n.success, this.i18n.activateAccount,
					lang.hitch(this, function() {
				this._resetForm();
				window.location.href = "/";
			}));
			} else {
				this._handleError(result.error);
			}
		},

		_handleError: function(error) {
@@ -254,15 +251,8 @@ define([
			//	tags:
			//		callback private

			this._emitEvt('LOADED');

			this._resetForm();

			// TODO: cambiar cuando esten unificados los errores de la api
			if (error.response && error.response.data) {
				error = error.response.data.error;
			}

			var msg = error.description;
			this._emitEvt('TRACK', {
				type: TRACK.type.exception,
+18 −3
Original line number Diff line number Diff line
@@ -2,16 +2,19 @@ define([
	"dojo/_base/declare"
	, "dojo/_base/lang"
	, "redmic/modules/base/_Module"
	, 'redmic/modules/base/_Persistence'
	, "redmic/modules/base/_Store"
	, "./_ModelItfc"
], function(
	declare
	, lang
	, _Module
	, _Persistence
	, _Store
	, _ModelItfc
) {
	return declare([_Module, _Store, _ModelItfc], {

	return declare([_Module, _Store, _Persistence, _ModelItfc], {
		//	summary:
		//		Módulo para trabajar con instancias de modelos.

@@ -34,7 +37,8 @@ define([
					VALUE_REMOVED: "valueRemoved",
					VALUE_REINDEXED: "valueReindexed",
					MODEL_BUILD: "modelBuild",
					GOT_MODEL_UUID: "gotModelUuid"
					GOT_MODEL_UUID: "gotModelUuid",
					SAVE_MODEL: 'saveModel'
				},
				actions: {
					SET_PROPERTY_VALUE: "setPropertyValue",
@@ -119,6 +123,9 @@ define([
			},{
				channel: this.getChannel("GET_MODEL_UUID"),
				callback: "_subGetModelUuid"
			},{
				channel: this.getChannel('SAVE'),
				callback: '_subSave'
			});
		},

@@ -163,6 +170,9 @@ define([
			},{
				event: 'GOT_MODEL_UUID',
				channel: this.getChannel("GOT_MODEL_UUID")
			},{
				event: 'SAVE_MODEL',
				channel: this.getChannel('SAVED')
			});
		},

@@ -281,6 +291,11 @@ define([
			this._emitEvt('GOT_MODEL_UUID', {
				uuid: this._getModelUuid(req)
			});
		},

		_subSave: function(req) {

			this._saveModel(req);
		}
	});
});
Loading