Commit 9bc64aa1 authored by Pedro Eduardo Trujillo's avatar Pedro Eduardo Trujillo
Browse files

Revisa, simplifica y corrige comportamiento de xhr

Unifica lógica de peticiones y hace posible implementar comportamientos
en función del código de respuesta procedente del servicio.
parent 200bab3b
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -136,7 +136,8 @@ define([

			queryResult.then(
				lang.hitch(this, function(req, result) {
					queryResult.total.then(lang.hitch(this, this._emitRequestResults, req, result));

					this._emitRequestResults(req, result, result.total);
				}, req),
				lang.hitch(this, this._emitError, target));

+122 −104
Original line number Diff line number Diff line
@@ -34,18 +34,18 @@ define([

			this.config = {
				timeout: 45000,
				type: "ES",
				defaultType: "ES",
				action: "_search",
				type: 'ES',
				defaultType: 'ES',
				action: '_search',
				headers: {},
				target: "",
				idProperty: "id",
				ascendingPrefix: "%2B",
				descendingPrefix: "-",
				target: '',
				idProperty: 'id',
				ascendingPrefix: '%2B',
				descendingPrefix: '-',
				queryEngine: SimpleQueryEngine,
				limitDefault: 100,
				handleAs: "json",
				accepts: "application/javascript, application/json"
				handleAs: 'json',
				accepts: 'application/javascript, application/json'
			};

			lang.mixin(this, this.config, args);
@@ -63,31 +63,39 @@ define([
			// returns: Object
			//		El item traido del servicio

			options = options || {};
			var deferred = new Deferred(),
			var responseDfd = new Deferred(),
				target = this.target + id,
				successCallback = lang.hitch(this, this._handleGetResponse, responseDfd),
				errorCallback = lang.hitch(this, this._handleGetError, responseDfd);

				xhrOptions = {
					method: "GET",
			var requestDfd = xhr(target, {
				method: 'GET',
				handleAs: this.handleAs,
					headers: this._getHeaders("get", options),
				headers: this._getHeaders('get', options || {}),
				timeout: this.timeout
			});

			requestDfd.response.then(successCallback, errorCallback);

			return responseDfd;
		},

				callback = lang.hitch(this, function(data) {
		_handleGetResponse: function(dfd, res) {

			var data = res.data,
				status = res.status;

			// TODO sustituir propiedad 'success' por el uso del status de la respuesta
			if (data.success || data.success === undefined) {
						deferred.resolve(data.body ? data.body : data);
				dfd.resolve(data.body || data);
			} else {
						deferred.reject(data);
				dfd.reject(data);
			}
				});

			xhr(target, xhrOptions).then(callback, lang.hitch(this, function(err) {
		},

				deferred.reject(this._parseError(err));
			}));
		_handleGetError: function(dfd, err) {

			return deferred;
			dfd.reject(this._parseError(err));
		},

		query: function(query, options) {
@@ -99,78 +107,105 @@ define([
			//
			// returns: dojo/store/api/Store.QueryResults

			if (this.type === this.defaultType) {
				this.target += this.action;
			}
			var responseDfd = new Deferred(),
				queryString = this._getQuery(query, options) || '',
				target = this.target + (this.type === this.defaultType ? this.action : '') + queryString,
				successCallback = lang.hitch(this, this._handleQueryResponse, responseDfd),
				errorCallback = lang.hitch(this, this._handleQueryError, responseDfd);

			var deferred = new Deferred(),
				queryString = this._getQuery(query, options) || "",
				results = xhr(this.target + queryString, {
					method: "GET",
			var requestDfd = xhr(target, {
				method: 'GET',
				handleAs: this.handleAs,
					headers: this._getHeaders("query", options),
				headers: this._getHeaders('query', options),
				timeout: this.timeout,
				query : queryString ? {} : query
			});

			deferred.total = new Deferred();
			requestDfd.response.then(successCallback, errorCallback);

			results.then (
				lang.hitch(this, function(rest) {
					if (rest.success) {
						if (this.type !== this.defaultType) {
							results.response.then(function(response) {
								deferred.resolve(rest.body);
								deferred.total.resolve(rest.total || rest.body.length);
							});
			return QueryResults(responseDfd);
		},

		_handleQueryResponse: function(dfd, res) {

			var data = res.data,
				status = res.status;

			// TODO sustituir propiedad 'success' por el uso del status de la respuesta
			if (data.success) {
				var response = data.body;

				// TODO parece funcionar bien sin este bloque, borrar si sigue todo correcto
				/*if (this.type !== this.defaultType) {
					response.total = data.total || data.body.length;
				} else {
							deferred.resolve(rest.body);
							deferred.total.resolve(rest.body.total || rest.body.length);
						}
					response.total = data.body.total || data.body.length;
				}*/

				dfd.resolve(response);
			} else {
						deferred.reject(lang.delegate(rest, {total: 0}));
					}
				}),
				function(error) {
					deferred.reject(lang.delegate(error, {total: 0}));
				dfd.reject(lang.delegate(data, {total: 0}));
			}
			);
			return QueryResults(deferred);
		},

		post: function(object, queryString, options) {
		_handleQueryError: function(dfd, err) {

			dfd.reject(lang.delegate(err, { total: 0 }));
		},

		post: function(queryObject, queryString, options) {
			// summary:
			//		Consultas vía post
			// object: Object
			// queryObject: Object
			//		Query object.
			// options: __PutDirectives?
			//
			// returns: dojo/_base/Deferred

			if (this.type === this.defaultType) {
				this.target += this.action;
			}

			options = options || {};
			var responseDfd = new Deferred(),
				target = this.target + (this.type === this.defaultType ? this.action : ''),
				successCallback = lang.hitch(this, this._handlePostResponse, responseDfd),
				errorCallback = lang.hitch(this, this._handlePostError, responseDfd);

			var self = this,
				deferred = new Deferred();

			deferred.total = new Deferred();

			xhr(this.target, {
				method: "POST",
				data: JSON.stringify(object),
			var requestDfd = xhr(target, {
				method: 'POST',
				data: JSON.stringify(queryObject),
				handleAs: this.handleAs,
				headers: self._getHeaders("post", options),
				headers: this._getHeaders('post', options || {}),
				timeout: this.timeout,
				query: queryString
			}).then(lang.hitch(this, this._responseParse, deferred), lang.hitch(this, function(err) {
			});

			requestDfd.response.then(successCallback, errorCallback);

			return responseDfd;
		},

				deferred.reject(this._parseError(err));
			}));
		_handlePostResponse: function(dfd, res) {

			return deferred;
			var data = res.data,
				status = res.status;

			// TODO sustituir propiedad 'success' por el uso del status de la respuesta
			if (data.success) {
				var response = data.body;

				// TODO parece funcionar bien sin este bloque, borrar si sigue todo correcto
				/*if (!this.type || this.type === this.defaultType) {
					response.total = response.length;
				} else {
					response.total = response.total || 0;
				}*/

				dfd.resolve(response);
			} else {
				dfd.reject(data);
			}
		},

		_handlePostError: function(dfd, err) {

			dfd.reject(this._parseError(err));
		},

		_parseError: function(err) {
@@ -187,26 +222,9 @@ define([
			return data.error || data;
		},

		_responseParse: function(deferred, data) {

			if (!data.success) {
				deferred.reject(data);
			}

			data = data.body;

			if (!this.type || this.type === this.defaultType) {
				deferred.resolve(data);
				deferred.total.resolve(data.length);
			} else {
				deferred.resolve(data);
				deferred.total.resolve(data.total ? data.total : 0);
			}
		},

		_getHeaders: function(type, options) {

			if (type === "query") {
			if (type === 'query') {
				var headers = lang.mixin({ Accept: this.accepts }, this.headers, options.headers);

				if (options.start >= 0 || options.count >= 0) {