Merge pull request #207 from probins/script

Change Protocol/Script registry to object
This commit is contained in:
Éric Lemoine
2012-03-05 13:52:44 -08:00
2 changed files with 32 additions and 19 deletions
+25 -18
View File
@@ -54,20 +54,12 @@ OpenLayers.Protocol.Script = OpenLayers.Class(OpenLayers.Protocol, {
callback: null, callback: null,
/** /**
* APIProperty: scope * APIProperty: callbackTemplate
* {Object} Optional ``this`` object for the callback. Read-only, set * {String} Template for creating a unique callback function name
* through the options passed to the constructor. * for the registry. Should include ${id}.
* Default is "OpenLayers.Protocol.Script.registry[${id}]".
*/ */
scope: null, callbackTemplate: "OpenLayers.Protocol.Script.registry[${id}]",
/**
* APIProperty: format
* {<OpenLayers.Format>} Format for parsing features. Default is an
* <OpenLayers.Format.GeoJSON> format. If an alternative is provided,
* the format's read method must take an object and return an array
* of features.
*/
format: null,
/** /**
* APIProperty: callbackKey * APIProperty: callbackKey
@@ -88,6 +80,22 @@ OpenLayers.Protocol.Script = OpenLayers.Class(OpenLayers.Protocol, {
*/ */
callbackPrefix: "", callbackPrefix: "",
/**
* APIProperty: scope
* {Object} Optional ``this`` object for the callback. Read-only, set
* through the options passed to the constructor.
*/
scope: null,
/**
* APIProperty: format
* {<OpenLayers.Format>} Format for parsing features. Default is an
* <OpenLayers.Format.GeoJSON> format. If an alternative is provided,
* the format's read method must take an object and return an array
* of features.
*/
format: null,
/** /**
* Property: pendingRequests * Property: pendingRequests
* {Object} References all pending requests. Property names are script * {Object} References all pending requests. Property names are script
@@ -212,7 +220,7 @@ OpenLayers.Protocol.Script = OpenLayers.Class(OpenLayers.Protocol, {
*/ */
createRequest: function(url, params, callback) { createRequest: function(url, params, callback) {
var id = OpenLayers.Protocol.Script.register(callback); var id = OpenLayers.Protocol.Script.register(callback);
var name = "OpenLayers.Protocol.Script.registry[" + id + "]"; var name = OpenLayers.String.format(this.callbackTemplate, {id: id});
params = OpenLayers.Util.extend({}, params); params = OpenLayers.Util.extend({}, params);
params[this.callbackKey] = this.callbackPrefix + name; params[this.callbackKey] = this.callbackPrefix + name;
url = OpenLayers.Util.urlAppend( url = OpenLayers.Util.urlAppend(
@@ -333,7 +341,7 @@ OpenLayers.Protocol.Script = OpenLayers.Class(OpenLayers.Protocol, {
(function() { (function() {
var o = OpenLayers.Protocol.Script; var o = OpenLayers.Protocol.Script;
var counter = 0; var counter = 0;
o.registry = []; o.registry = {};
/** /**
* Function: OpenLayers.Protocol.Script.register * Function: OpenLayers.Protocol.Script.register
@@ -345,12 +353,11 @@ OpenLayers.Protocol.Script = OpenLayers.Class(OpenLayers.Protocol, {
* that is the JSON returned by the service. * that is the JSON returned by the service.
* *
* Returns: * Returns:
* {Number} An identifier for retreiving the registered callback. * {Number} An identifier for retrieving the registered callback.
*/ */
o.register = function(callback) { o.register = function(callback) {
var id = ++counter; var id = "c"+(++counter);
o.registry[id] = function() { o.registry[id] = function() {
o.unregister(id);
callback.apply(this, arguments); callback.apply(this, arguments);
}; };
return id; return id;
+7 -1
View File
@@ -135,7 +135,7 @@
} }
function test_createRequest(t) { function test_createRequest(t) {
t.plan(3); t.plan(4);
var protocol = new OpenLayers.Protocol.Script({ var protocol = new OpenLayers.Protocol.Script({
callbackKey: 'cb_key', callbackKey: 'cb_key',
callbackPrefix: 'cb_prefix:' callbackPrefix: 'cb_prefix:'
@@ -155,7 +155,13 @@
t.eq(script.id, 'OpenLayers_Protocol_Script_bar', t.eq(script.id, 'OpenLayers_Protocol_Script_bar',
'created script has a correct id'); 'created script has a correct id');
protocol.callbackTemplate = "OpenLayers.Protocol.Script.registry.${id}";
script = protocol.createRequest('http://bar_url/', {'k': 'bar_param'}, 'bar_callback');
t.eq(script.src, 'http://bar_url/?k=bar_param&cb_key=cb_prefix%3AOpenLayers.Protocol.Script.registry.bar',
'created script has a correct url with different template');
OpenLayers.Protocol.Script.register = _register; OpenLayers.Protocol.Script.register = _register;
} }
function test_destroyRequest(t) { function test_destroyRequest(t) {