Loading strategies now trigger loadstart and loadend events. r=crschmidt (closes #1840)

git-svn-id: http://svn.openlayers.org/trunk/openlayers@8973 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
Tim Schaub
2009-03-09 22:17:23 +00:00
parent 97f4ad54a3
commit 837efd0b9c
4 changed files with 117 additions and 14 deletions

View File

@@ -176,6 +176,7 @@ OpenLayers.Strategy.BBOX = OpenLayers.Class(OpenLayers.Strategy, {
*/ */
triggerRead: function() { triggerRead: function() {
this.layer.protocol.abort(this.response); this.layer.protocol.abort(this.response);
this.layer.events.triggerEvent("loadstart");
this.response = this.layer.protocol.read({ this.response = this.layer.protocol.read({
filter: this.createFilter(), filter: this.createFilter(),
callback: this.merge, callback: this.merge,
@@ -231,6 +232,7 @@ OpenLayers.Strategy.BBOX = OpenLayers.Class(OpenLayers.Strategy, {
} }
this.layer.addFeatures(features); this.layer.addFeatures(features);
} }
this.layer.events.triggerEvent("loadend");
}, },
CLASS_NAME: "OpenLayers.Strategy.BBOX" CLASS_NAME: "OpenLayers.Strategy.BBOX"

View File

@@ -71,6 +71,7 @@ OpenLayers.Strategy.Fixed = OpenLayers.Class(OpenLayers.Strategy, {
* Tells protocol to load data and unhooks the visibilitychanged event * Tells protocol to load data and unhooks the visibilitychanged event
*/ */
load: function() { load: function() {
this.layer.events.triggerEvent("loadstart");
this.layer.protocol.read({ this.layer.protocol.read({
callback: this.merge, callback: this.merge,
scope: this scope: this
@@ -101,6 +102,7 @@ OpenLayers.Strategy.Fixed = OpenLayers.Class(OpenLayers.Strategy, {
} }
this.layer.addFeatures(features); this.layer.addFeatures(features);
} }
this.layer.events.triggerEvent("loadend");
}, },
CLASS_NAME: "OpenLayers.Strategy.Fixed" CLASS_NAME: "OpenLayers.Strategy.Fixed"

View File

@@ -83,6 +83,42 @@
t.ok(strategy.bounds.equals(map.getExtent().transform(from, to)), "[force update different proj] bounds transformed"); t.ok(strategy.bounds.equals(map.getExtent().transform(from, to)), "[force update different proj] bounds transformed");
}
function test_events(t) {
t.plan(2);
var log = {
loadstart: 0,
loadend: 0
};
var map = new OpenLayers.Map("map");
var layer = new OpenLayers.Layer.Vector(null, {
strategies: [new OpenLayers.Strategy.BBOX()],
protocol: new OpenLayers.Protocol({
read: function(config) {
config.callback.call(config.scope, {});
}
}),
isBaseLayer: true,
eventListeners: {
loadstart: function() {
++log.loadstart;
},
loadend: function() {
++log.loadend;
}
}
});
map.addLayer(layer);
map.zoomToMaxExtent();
t.eq(log.loadstart, 1, "loadstart triggered");
t.eq(log.loadend, 1, "loadend triggered");
map.destroy();
} }
function test_triggerRead(t) { function test_triggerRead(t) {
@@ -96,26 +132,38 @@
return filter; return filter;
}; };
s.response = {"fake": "response"}; s.response = {"fake": "response"};
var log = {};
var protocol = new OpenLayers.Protocol({ var protocol = new OpenLayers.Protocol({
read: function(options) { read: function(options) {
t.ok(options.filter == filter, log.options = options;
"protocol read called with correct filter");
t.ok(options.callback == s.merge,
"protocol read called with correct callback");
t.ok(options.scope == s,
"protocol read called with correct scope");
}, },
abort: function(response) { abort: function(response) {
t.eq(response, s.response, log.abort = response.fake;
"protocol abort called with correct response");
} }
}); });
var layer = new OpenLayers.Layer.Vector(null, {
strategies: [s],
protocol: protocol,
isBaseLayer: true
});
var map = new OpenLayers.Map("map");
map.addLayer(layer);
map.zoomToMaxExtent();
t.ok(log.options.filter == filter,
"protocol read called with correct filter");
t.ok(log.options.callback == s.merge,
"protocol read called with correct callback");
t.ok(log.options.scope == s,
"protocol read called with correct scope");
t.eq(log.abort, "response",
"protocol abort called with correct response");
s.setLayer({protocol: protocol}); map.destroy();
// 4 tests
s.triggerRead();
} }
function test_createFilter(t) { function test_createFilter(t) {

View File

@@ -60,21 +60,69 @@
"visibilitychanged listener unregistered"); "visibilitychanged listener unregistered");
} }
function tests_merge(t) { function test_events(t) {
t.plan(4); t.plan(2);
var log = {
loadstart: 0,
loadend: 0
};
var map = new OpenLayers.Map("map");
var layer = new OpenLayers.Layer.Vector(null, {
strategies: [new OpenLayers.Strategy.Fixed()],
protocol: new OpenLayers.Protocol({
read: function(config) {
config.callback.call(config.scope, {});
}
}),
isBaseLayer: true,
eventListeners: {
loadstart: function() {
++log.loadstart;
},
loadend: function() {
++log.loadend;
}
}
});
map.addLayer(layer);
map.zoomToMaxExtent();
t.eq(log.loadstart, 1, "loadstart triggered");
t.eq(log.loadend, 1, "loadend triggered");
map.destroy();
}
function test_merge(t) {
t.plan(5);
var strategy = new OpenLayers.Strategy.Fixed(); var strategy = new OpenLayers.Strategy.Fixed();
// create map with default projection // create map with default projection
var map = new OpenLayers.Map("map"); var map = new OpenLayers.Map("map");
var log = {
loadend: 0
};
// create layer with custom projection // create layer with custom projection
var layer = new OpenLayers.Layer.Vector(null, { var layer = new OpenLayers.Layer.Vector(null, {
isBaseLayer: true, isBaseLayer: true,
strategies: [strategy], strategies: [strategy],
protocol: new OpenLayers.Protocol(), protocol: new OpenLayers.Protocol(),
projection: new OpenLayers.Projection("EPSG:900913") projection: new OpenLayers.Projection("EPSG:900913"),
eventListeners: {
loadend: function() {
++log.loadend;
}
}
}); });
map.addLayer(layer); map.addLayer(layer);
map.zoomToMaxExtent(); map.zoomToMaxExtent();
@@ -92,6 +140,9 @@
// call merge with a mocked up response // call merge with a mocked up response
strategy.merge({features: features}); strategy.merge({features: features});
// confirm that loadend was called
t.eq(log.loadend, 1, "merge triggers loadend");
// test that feature geometries have been transformed to map projection // test that feature geometries have been transformed to map projection
var from = layer.projection; var from = layer.projection;
var to = map.getProjectionObject(); var to = map.getProjectionObject();