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

Implementa obtención de subschemas para modelos

Gracias a esto, no será necesario definir controladores desde el lado de
servidor para obtener schemas que ya se han devuelto anidados en otros
mayores previamente (por ejemplo, resources dentro de activity).
Incluye tests unitarios.
parent 95249537
Loading
Loading
Loading
Loading
+17 −3
Original line number Diff line number Diff line
@@ -38,7 +38,8 @@ define([
					VALUE_REINDEXED: "valueReindexed",
					MODEL_BUILD: "modelBuild",
					GOT_MODEL_UUID: "gotModelUuid",
					SAVE_MODEL: 'saveModel'
					SAVE_MODEL: 'saveModel',
					GOT_PROPERTY_SCHEMA: "gotPropertySchema"
				},
				actions: {
					SET_PROPERTY_VALUE: "setPropertyValue",
@@ -67,7 +68,9 @@ define([
					REMEMBER_CURRENT_VALUE: "rememberCurrentValue",
					MODEL_BUILD: "modelBuild",
					GET_MODEL_UUID: "getModelUuid",
					GOT_MODEL_UUID: "gotModelUuid"
					GOT_MODEL_UUID: "gotModelUuid",
					GOT_PROPERTY_SCHEMA: "gotPropertySchema",
					GET_PROPERTY_SCHEMA: "getPropertySchema"
				},
				idForGet: '_schema'
			};
@@ -126,6 +129,9 @@ define([
			},{
				channel: this.getChannel('SAVE'),
				callback: '_subSave'
			},{
				channel: this.getChannel("GET_PROPERTY_SCHEMA"),
				callback: "_subGetPropertySchema"
			});
		},

@@ -173,6 +179,9 @@ define([
			},{
				event: 'SAVE_MODEL',
				channel: this.getChannel('SAVED')
			},{
				event: 'GOT_PROPERTY_SCHEMA',
				channel: this.getChannel("GOT_PROPERTY_SCHEMA")
			});
		},

@@ -296,6 +305,11 @@ define([
		_subSave: function(req) {

			this._saveModel(req);
		}
		},

		_subGetPropertySchema: function(req) {

			this._getPropertySchema(req);
		},
	});
});
+66 −0
Original line number Diff line number Diff line
@@ -233,6 +233,58 @@ define([
			dfd.resolve(propertyInstance);
		},

		_obtainPropertySchema: function(key) {

			var keySplit = key.split(this.pathSeparator),
				dfd = new Deferred(),
				action = lang.hitch(this, function(propKeySplit, schemaDfd) {

					this._findPropertySchema({
						pathArray: propKeySplit,
						dfd: schemaDfd
					}, this.modelInstance);
				}, keySplit, dfd);

			this._doActionWhenBuilt(action);

			return dfd;
		},

		_findPropertySchema: function(obj, propertyInstance) {

			var pathArray = obj.pathArray,
				dfd = obj.dfd,
				schema = propertyInstance.get('schema');

			for (var i = 0; i < pathArray.length; i++) {
				var pathItem = pathArray[i],
					schemaType = schema.type;

				if (!pathItem.length) {
					break;
				}

				if (schemaType === 'object' || (schemaType instanceof Array && schemaType.indexOf('object') !== -1)) {
					schema = schema.properties;
				}

				if (pathItem === '{i}' && (schemaType === 'array' || (schemaType instanceof Array && schemaType.indexOf('array') !== -1))) {
					schema = schema.items;
				} else {
					schema = schema[pathItem];
				}

				if (!schema) {
					console.error("Tried to get schema of missing property '%s' at model '%s'", pathItem,
						this.getChannel());

					break;
				}
			}

			dfd.resolve(schema);
		},

		_reset: function(req) {

			var action = lang.hitch(this, function(req) {
@@ -328,6 +380,20 @@ define([
			}, key));
		},

		_getPropertySchema: function(req) {

			var key = req.key || '',
				dfd = this._obtainPropertySchema(key);

			dfd.then(lang.hitch(this, function(propKey, schema) {

				this._emitEvt('GOT_PROPERTY_SCHEMA', {
					schema: schema,
					propertyName: propKey
				});
			}, key));
		},

		_rememberCurrentValue: function(req) {

			this.modelInstance.reinitializeWithCurrentValue();
+2 −1
Original line number Diff line number Diff line
@@ -30,7 +30,8 @@ define([
				"_getPropertyInstance": {},
				"_getIdPropertyValue": {},
				"_addValue": {},
				"_deleteValue": {}
				"_deleteValue": {},
				"_getPropertySchema": {}
			});
		}
	});
+28 −3
Original line number Diff line number Diff line
@@ -159,8 +159,33 @@ define([
				Mediator.publish(modelInstance.getChannel('DESERIALIZE'), value);

				Mediator.publish(modelInstance.getChannel('SERIALIZE'));
			}
			},

			Should_ReturnOriginalSchema_When_GetPropertySchema: function() {

				var dfd = this.async(timeout);

				Mediator.once(modelInstance.getChannel('GOT_PROPERTY_SCHEMA'), dfd.callback(function(res) {

					assert.deepEqual(res.schema, schema.properties.name, 'El esquema obtenido no es el original');
				}));

				Mediator.publish(modelInstance.getChannel('GET_PROPERTY_SCHEMA'), {
					key: 'name'
				});
			},

			Should_ReturnOriginalSchema_When_GetRootSchema: function() {

				var dfd = this.async(timeout);

				Mediator.once(modelInstance.getChannel('GOT_PROPERTY_SCHEMA'), dfd.callback(function(res) {

					assert.deepEqual(res.schema, schema, 'El esquema obtenido no es el original');
				}));

				Mediator.publish(modelInstance.getChannel('GET_PROPERTY_SCHEMA'), {});
			}
		}
	});
});