Sending a response object along to the user callback after a commit that issues multiple requests. Thanks for the collaborative patch work crschmidt. r=crschmidt,elemoine (closes #1973)

git-svn-id: http://svn.openlayers.org/trunk/openlayers@9246 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
Tim Schaub
2009-04-08 17:52:55 +00:00
parent 861a9c10f5
commit 0e858fa4af
2 changed files with 76 additions and 105 deletions

View File

@@ -412,12 +412,13 @@ OpenLayers.Protocol.HTTP = OpenLayers.Class(OpenLayers.Protocol, {
types[OpenLayers.State.INSERT] = [];
types[OpenLayers.State.UPDATE] = [];
types[OpenLayers.State.DELETE] = [];
var feature, list;
var feature, list, requestFeatures = [];
for(var i=0, len=features.length; i<len; ++i) {
feature = features[i];
list = types[feature.state];
if(list) {
list.push(feature);
requestFeatures.push(feature);
}
}
// tally up number of requests
@@ -425,19 +426,43 @@ OpenLayers.Protocol.HTTP = OpenLayers.Class(OpenLayers.Protocol, {
types[OpenLayers.State.UPDATE].length +
types[OpenLayers.State.DELETE].length;
function callback(response) {
nResponses++;
response.last = (nResponses >= nRequests);
this.callUserCallback(response, options);
}
// This response will be sent to the final callback after all the others
// have been fired.
var success = true;
var finalResponse = new OpenLayers.Protocol.Response({
reqFeatures: requestFeatures
});
function insertCallback(response) {
var len = response.features ? response.features.length : 0;
var fids = new Array(len);
for(var i=0; i<len; ++i) {
fids[i] = response.features[i].fid;
}
finalResponse.insertIds = fids;
callback.apply(this, [response]);
}
function callback(response) {
this.callUserCallback(response, options);
success = success && response.success();
nResponses++;
if (nResponses >= nRequests) {
if (options.callback) {
finalResponse.code = success ?
OpenLayers.Protocol.Response.SUCCESS :
OpenLayers.Protocol.Response.FAILURE;
options.callback.apply(options.scope, [finalResponse]);
}
}
}
// start issuing requests
var queue = types[OpenLayers.State.INSERT];
if(queue.length > 0) {
resp.push(this.create(
queue, OpenLayers.Util.applyDefaults(
{callback: callback, scope: this},
options.create || {} // remove || when #1716 is resolved
{callback: insertCallback, scope: this}, options.create
)
));
}
@@ -445,8 +470,7 @@ OpenLayers.Protocol.HTTP = OpenLayers.Class(OpenLayers.Protocol, {
for(var i=queue.length-1; i>=0; --i) {
resp.push(this.update(
queue[i], OpenLayers.Util.applyDefaults(
{callback: callback, scope: this},
options.update || {} // remove || when #1716 is resolved
{callback: callback, scope: this}, options.update
))
);
}
@@ -454,8 +478,7 @@ OpenLayers.Protocol.HTTP = OpenLayers.Class(OpenLayers.Protocol, {
for(var i=queue.length-1; i>=0; --i) {
resp.push(this["delete"](
queue[i], OpenLayers.Util.applyDefaults(
{callback: callback, scope: this},
options["delete"] || {} // remove || when #1716 is resolved
{callback: callback, scope: this}, options["delete"]
))
);
}
@@ -492,9 +515,6 @@ OpenLayers.Protocol.HTTP = OpenLayers.Class(OpenLayers.Protocol, {
if(opt && opt.callback) {
opt.callback.call(opt.scope, resp);
}
if(resp.last && options.callback) {
options.callback.call(options.scope);
}
},
CLASS_NAME: "OpenLayers.Protocol.HTTP"