This commit is contained in:
Alessandro Isaacs
2016-03-30 09:00:47 -07:00
parent 4a33db370b
commit 85f11b093d
+27 -21
View File
@@ -1,10 +1,6 @@
goog.provide('ol.source.CartoDB'); goog.provide('ol.source.CartoDB');
goog.require('goog.asserts'); goog.require('ol.source.State');
goog.require('goog.events');
goog.require('goog.net.EventType');
goog.require('goog.net.XhrIo');
goog.require('goog.net.XhrIo.ResponseType');
goog.require('ol.source.XYZ'); goog.require('ol.source.XYZ');
@@ -82,15 +78,12 @@ ol.source.CartoDB.prototype.initializeMap_ = function() {
mapUrl += '/named/' + this.mapId_; mapUrl += '/named/' + this.mapId_;
} }
var xhrIo = new goog.net.XhrIo(); var client = new XMLHttpRequest();
xhrIo.setResponseType(goog.net.XhrIo.ResponseType.TEXT); client.addEventListener('load', this.handleInitResponse_.bind(this, paramHash));
xhrIo.setWithCredentials(false); client.addEventListener('error', this.handleInitError_.bind(this));
goog.events.listen(xhrIo, goog.net.EventType.COMPLETE, client.open('POST', mapUrl);
this.handleInitResponse_.bind(this, paramHash)); client.setRequestHeader('Content-type', 'application/json');
xhrIo.send(mapUrl, client.send(JSON.stringify(this.config_));
'POST',
JSON.stringify(this.config_),
{'Content-Type': 'application/json'});
}; };
@@ -102,16 +95,29 @@ ol.source.CartoDB.prototype.initializeMap_ = function() {
* @private * @private
*/ */
ol.source.CartoDB.prototype.handleInitResponse_ = function(paramHash, event) { ol.source.CartoDB.prototype.handleInitResponse_ = function(paramHash, event) {
var xhrIo = event.target; var client = /** @type {XMLHttpRequest} */ (event.target);
goog.asserts.assertInstanceof(xhrIo, goog.net.XhrIo, if (client.status >= 200 && client.status < 300) {
'event.target/xhrIo is an instance of goog.net.XhrIo'); var response;
var data = xhrIo.getResponseJson(); try {
if (xhrIo.isSuccess()) { response = /** @type {Object} */(JSON.parse(client.responseText));
this.applyTemplate_(data); } catch (err) {
this.setState(ol.source.State.ERROR);
return;
}
this.applyTemplate_(response);
this.templateCache_[paramHash] = response;
} else {
this.setState(ol.source.State.ERROR);
} }
this.templateCache_[paramHash] = data;
}; };
/**
* @private
* @param {Event} event Event.
*/
ol.source.CartoDB.prototype.handleInitError_ = function(event) {
this.setState(ol.source.State.ERROR);
}
/** /**
* Apply the new tile urls returned by carto db * Apply the new tile urls returned by carto db