Skip to content

Commit

Permalink
Merge pull request Esri#354 from patrickarlt/request-updates
Browse files Browse the repository at this point in the history
Request enhancements
  • Loading branch information
patrickarlt committed Sep 30, 2014
2 parents b73b80d + 952f9d7 commit 8358c0f
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 27 deletions.
1 change: 1 addition & 0 deletions karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ module.exports = function(config) {
'http://cdn.leafletjs.com/leaflet-0.7.3/leaflet-src.js',
'http://cdn-geoweb.s3.amazonaws.com/Leaflet.heat/0.1.1/leaflet-heat.js',
'http://cdn-geoweb.s3.amazonaws.com/Leaflet.markercluster/0.4.0/leaflet.markercluster-src.js',
// 'spec/Layers/FeatureLayer/FeatureManagerSpec.js',
'spec/**/*Spec.js',
'src/EsriLeaflet.js',
'src/Util.js',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,19 +195,19 @@ In additon to these events `L.esri.FeatureLayer` also fires the following [Mouse
<td>Returns the current `where` setting</td>
</tr>
<tr>
<td><code>setWhere({{{param 'String' 'where'}}})</code></td>
<td><code>setWhere({{{param 'String' 'where'}}}, {{{param 'Function' 'callback'}}}, {{{param 'Object' 'context'}}})</code></td>
<td><code>this</code></td>
<td>Sets the new `where` option and refreshes the layer to reflect the new <code>where</code> filter.</td>
<td>Sets the new `where` option and refreshes the layer to reflect the new <code>where</code> filter. Accepts an optional callback and function context.</td>
</tr>
<tr>
<td><code>getTimeRange()</code></td>
<td><code>Array</code></td>
<td>Returns the current time range as an array like <code>[from, to]</code></td>
</tr>
<tr>
<td><code>setTimeRange({{{param 'Date' 'from'}}}, {{{param 'Date' 'to'}}})</code></td>
<td><code>setTimeRange({{{param 'Date' 'from'}}}, {{{param 'Date' 'to'}}}, , {{{param 'Function' 'callback'}}}, {{{param 'Object' 'context'}}})</code></td>
<td><code>this</code></td>
<td>Sets the current time filter applied to features. An optional callback is run upon completion only if <code>timeFilterMode</code> is set to <code>'server'</code>.</td>
<td>Sets the current time filter applied to features. An optional callback is run upon completion if <code>timeFilterMode</code> is set to <code>'server'</code>. Also accepts function context as the last argument.</td>
</tr>
<tr>
<td><code>authenticate({{{param 'String' 'token'}}})</code></td>
Expand Down
8 changes: 4 additions & 4 deletions site/source/pages/api-reference/layers/feature-layer.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,19 +211,19 @@ In additon to these events `L.esri.Layer.FeatureLayer` also fires the following
<td>Returns the current `where` setting</td>
</tr>
<tr>
<td><code>setWhere({{{param 'String' 'where'}}})</code></td>
<td><code>setWhere({{{param 'String' 'where'}}}, {{{param 'Function' 'callback'}}}, {{{param 'Object' 'context'}}})</code></td>
<td><code>this</code></td>
<td>Sets the new `where` option and refreshes the layer to reflect the new <code>where</code> filter.</td>
<td>Sets the new `where` option and refreshes the layer to reflect the new <code>where</code> filter. Accepts an optional callback and function context.</td>
</tr>
<tr>
<td><code>getTimeRange()</code></td>
<td><code>Array</code></td>
<td>Returns the current time range as an array like <code>[from, to]</code></td>
</tr>
<tr>
<td><code>setTimeRange({{{param 'Date' 'from'}}}, {{{param 'Date' 'to'}}})</code></td>
<td><code>setTimeRange({{{param 'Date' 'from'}}}, {{{param 'Date' 'to'}}}, , {{{param 'Function' 'callback'}}}, {{{param 'Object' 'context'}}})</code></td>
<td><code>this</code></td>
<td>Sets the current time filter applied to features. An optional callback is run upon completion only if <code>timeFilterMode</code> is set to <code>'server'</code>.</td>
<td>Sets the current time filter applied to features. An optional callback is run upon completion if <code>timeFilterMode</code> is set to <code>'server'</code>. Also accepts function context as the last argument.</td>
</tr>
<tr>
<td><code>authenticate({{{param 'String' 'token'}}})</code></td>
Expand Down
10 changes: 5 additions & 5 deletions site/source/pages/api-reference/layers/heatmap-feature-layer.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,20 +116,20 @@ More information about Feature Layers can be found in the [`L.esri.Layers.Featur
<td><code>String</code></td>
<td>Returns the current `where` setting</td>
</tr>
<tr>
<td><code>setWhere({{{param 'String' 'where'}}})</code></td>
<tr>
<td><code>setWhere({{{param 'String' 'where'}}}, {{{param 'Function' 'callback'}}}, {{{param 'Object' 'context'}}})</code></td>
<td><code>this</code></td>
<td>Sets the new `where` option and refreshes the layer to reflect the new <code>where</code> filter.</td>
<td>Sets the new `where` option and refreshes the layer to reflect the new <code>where</code> filter. Accepts an optional callback and function context.</td>
</tr>
<tr>
<td><code>getTimeRange()</code></td>
<td><code>Array</code></td>
<td>Returns the current time range as an array like <code>[from, to]</code></td>
</tr>
<tr>
<td><code>setTimeRange({{{param 'Date' 'from'}}}, {{{param 'Date' 'to'}}})</code></td>
<td><code>setTimeRange({{{param 'Date' 'from'}}}, {{{param 'Date' 'to'}}}, , {{{param 'Function' 'callback'}}}, {{{param 'Object' 'context'}}})</code></td>
<td><code>this</code></td>
<td>Sets the current time filter applied to features. An optional callback is run upon completion only if <code>timeFilterMode</code> is set to <code>'server'</code>.</td>
<td>Sets the current time filter applied to features. An optional callback is run upon completion if <code>timeFilterMode</code> is set to <code>'server'</code>. Also accepts function context as the last argument.</td>
</tr>
<tr>
<td><code>authenticate({{{param 'String' 'token'}}})</code></td>
Expand Down
11 changes: 8 additions & 3 deletions spec/Layers/FeatureLayer/FeatureManagerSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -410,12 +410,14 @@ describe('L.esri.Layers.FeatureManager', function () {
objectIdFieldName: 'OBJECTID'
}));

layer.setTimeRange(new Date('January 13 2014 GMT-0800'), new Date('January 16 2014 GMT-0800'));
var callback = sinon.spy();

layer.setTimeRange(new Date('January 13 2014 GMT-0800'), new Date('January 16 2014 GMT-0800'), callback);

server.respond();

expect(callback).to.have.been.called;
expect(layer.removeLayers).to.have.been.calledWith([4, 4]);

expect(layer.createLayers).to.have.been.calledWith([{
'geometry': {
'type': 'Point',
Expand Down Expand Up @@ -466,10 +468,13 @@ describe('L.esri.Layers.FeatureManager', function () {
'id': 1
}]);

layer.setWhere('Type="Inactive"');
var callback = sinon.spy();

layer.setWhere('Type="Inactive"', callback);

server.respond();

expect(callback).to.have.been.called;
expect(layer.removeLayers).to.have.been.calledWith([1]);
expect(layer.createLayers).to.have.been.calledWith([{
'type': 'Feature',
Expand Down
6 changes: 3 additions & 3 deletions spec/RequestSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ describe('L.esri.Request', function () {
foo: 'bar'
});

request(sampleResponse);
L.esri._callback[request.id](sampleResponse);
});

it('should callback with an error on non-JSON reponses', function(done){
Expand All @@ -69,7 +69,7 @@ describe('L.esri.Request', function () {
done();
});

request('foo');
L.esri._callback[request.id]('foo');
});

it('should callback with an error when an error is recived from the server', function(done){
Expand All @@ -78,7 +78,7 @@ describe('L.esri.Request', function () {
done();
});

request(sampleError);
L.esri._callback[request.id](sampleError);
});

it('should be able to make a POST request with CORS', function(done){
Expand Down
2 changes: 1 addition & 1 deletion spec/Services/ServiceSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ describe('L.esri.Service', function () {
done();
});

request({foo:'bar'});
L.esri._callback[request.id]({foo:'bar'});
});

it('should make POST requests', function(done){
Expand Down
32 changes: 27 additions & 5 deletions src/Layers/FeatureLayer/FeatureManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@

this._currentSnapshot = []; // cache of what layers should be active
this._activeRequests = 0;
this._pendingRequests = [];
},

/**
Expand Down Expand Up @@ -83,7 +84,7 @@
});
}

this._buildQuery(bounds).run(function(error, featureCollection, response){
return this._buildQuery(bounds).run(function(error, featureCollection, response){
if(response && response.exceededTransferLimit){
this.fire('drawlimitexceeded');
}
Expand Down Expand Up @@ -139,12 +140,19 @@
* Where Methods
*/

setWhere: function(where){
setWhere: function(where, callback, context){

this.options.where = (where && where.length) ? where : '1=1';

var oldSnapshot = [];
var newShapshot = [];
var pendingRequests = 0;
var requestError = null;
var requestCallback = L.Util.bind(function(error, featureCollection){
if(error){
requestError = error;
}

if(featureCollection){
for (var i = featureCollection.features.length - 1; i >= 0; i--) {
newShapshot.push(featureCollection.features[i].id);
Expand All @@ -157,6 +165,9 @@
this._currentSnapshot = newShapshot;
this.removeLayers(oldSnapshot);
this.addLayers(newShapshot);
if(callback) {
callback.call(context, requestError);
}
}
}, this);

Expand Down Expand Up @@ -186,12 +197,22 @@
return [this.options.from, this.options.to];
},

setTimeRange: function(from, to){
setTimeRange: function(from, to, callback, context){
var oldFrom = this.options.from;
var oldTo = this.options.to;

var requestCallback = L.Util.bind(function(){
var pendingRequests = 0;
var requestError = null;
var requestCallback = L.Util.bind(function(error){
if(error){
requestError = error;
}
this._filterExistingFeatures(oldFrom, oldTo, from, to);

pendingRequests--;

if(callback && pendingRequests <= 0){
callback.call(context, requestError);
}
}, this);

this.options.from = from;
Expand All @@ -201,6 +222,7 @@

if(this.options.timeFilterMode === 'server') {
for(var key in this._activeCells){
pendingRequests++;
var coords = this._keyToCellCoords(key);
var bounds = this._cellCoordsToBounds(coords);
this._requestFeatures(bounds, key, requestCallback);
Expand Down
12 changes: 10 additions & 2 deletions src/Request.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@
httpRequest.onreadystatechange = function(){
var response;
var error;

if (httpRequest.readyState === 4) {
try {
response = JSON.parse(httpRequest.responseText);

} catch(e) {
response = null;
error = {
Expand Down Expand Up @@ -146,7 +146,15 @@

callbacks++;

return L.esri._callback[callbackId];
return {
id: callbackId,
abort: function(){
L.esri._callback[callbackId]({
code: 500,
message: 'Could not parse response as JSON.'
});
}
};
}
}

Expand Down

0 comments on commit 8358c0f

Please sign in to comment.