From 127abf878291a08d501e619581ddaedd2a5e76f9 Mon Sep 17 00:00:00 2001 From: tschaub Date: Tue, 20 Mar 2012 17:07:52 -0600 Subject: [PATCH 1/2] Correcting the callback for the script protocol. In f08119b01e81cd03296d687ce916c05ee13d4e34, the default syntax for the script protocol's callback made it so the callbacks looked like `OpenLayers.Protocol.Script.registry[c1]`. This essentially breaks the script protocol everywhere except where `c1` is defined and equals "c1" (the string). Tests with this change demonstrate that default callbacks will now look like `OpenLayers.Protocol.Script.registry['c1']` (properly accessing the `c1` property of the registry object). --- lib/OpenLayers/Protocol/Script.js | 4 ++-- tests/Protocol/Script.html | 9 ++++++--- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/lib/OpenLayers/Protocol/Script.js b/lib/OpenLayers/Protocol/Script.js index 5cba843e9b..4ef847494e 100644 --- a/lib/OpenLayers/Protocol/Script.js +++ b/lib/OpenLayers/Protocol/Script.js @@ -57,9 +57,9 @@ OpenLayers.Protocol.Script = OpenLayers.Class(OpenLayers.Protocol, { * APIProperty: callbackTemplate * {String} Template for creating a unique callback function name * for the registry. Should include ${id}. - * Default is "OpenLayers.Protocol.Script.registry[${id}]". + * Default is "OpenLayers.Protocol.Script.registry['${id}']". */ - callbackTemplate: "OpenLayers.Protocol.Script.registry[${id}]", + callbackTemplate: "OpenLayers.Protocol.Script.registry['${id}']", /** * APIProperty: callbackKey diff --git a/tests/Protocol/Script.html b/tests/Protocol/Script.html index ee8ac89018..3a0b9b610f 100644 --- a/tests/Protocol/Script.html +++ b/tests/Protocol/Script.html @@ -135,7 +135,7 @@ } function test_createRequest(t) { - t.plan(4); + t.plan(5); var protocol = new OpenLayers.Protocol.Script({ callbackKey: 'cb_key', callbackPrefix: 'cb_prefix:' @@ -150,8 +150,11 @@ t.eq(script.type, 'text/javascript', 'created script has a correct type'); - t.eq(script.src, 'http://bar_url/?k=bar_param&cb_key=cb_prefix%3AOpenLayers.Protocol.Script.registry%5Bbar%5D', - 'created script has a correct url'); + + var params = OpenLayers.Util.getParameters(script.src); + t.eq(params.k, "bar_param", "custom query string param"); + t.eq(params.cb_key, "cb_prefix:OpenLayers.Protocol.Script.registry['bar']", "callback with prefix"); + t.eq(script.id, 'OpenLayers_Protocol_Script_bar', 'created script has a correct id'); From 609e5f7f09da0fa52d49714dd6e8ec8e10138e7e Mon Sep 17 00:00:00 2001 From: tschaub Date: Wed, 21 Mar 2012 09:18:23 -0600 Subject: [PATCH 2/2] Taking brackets out of the callback. The Yahoo API doesn't accept quotes in the callback (e.g. foo['bar']). Since the callback registry is an object and all identifiers are prefixed with "c" now, we can use dot notation. --- lib/OpenLayers/Protocol/Script.js | 7 ++++--- tests/Protocol/Script.html | 14 ++++++++------ 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/lib/OpenLayers/Protocol/Script.js b/lib/OpenLayers/Protocol/Script.js index 4ef847494e..f2ea5bcb24 100644 --- a/lib/OpenLayers/Protocol/Script.js +++ b/lib/OpenLayers/Protocol/Script.js @@ -56,10 +56,11 @@ OpenLayers.Protocol.Script = OpenLayers.Class(OpenLayers.Protocol, { /** * APIProperty: callbackTemplate * {String} Template for creating a unique callback function name - * for the registry. Should include ${id}. - * Default is "OpenLayers.Protocol.Script.registry['${id}']". + * for the registry. Should include ${id}. The ${id} variable will be + * replaced with a string identifier prefixed with a "c" (e.g. c1, c2). + * Default is "OpenLayers.Protocol.Script.registry.${id}". */ - callbackTemplate: "OpenLayers.Protocol.Script.registry['${id}']", + callbackTemplate: "OpenLayers.Protocol.Script.registry.${id}", /** * APIProperty: callbackKey diff --git a/tests/Protocol/Script.html b/tests/Protocol/Script.html index 3a0b9b610f..894427a605 100644 --- a/tests/Protocol/Script.html +++ b/tests/Protocol/Script.html @@ -135,7 +135,7 @@ } function test_createRequest(t) { - t.plan(5); + t.plan(6); var protocol = new OpenLayers.Protocol.Script({ callbackKey: 'cb_key', callbackPrefix: 'cb_prefix:' @@ -153,15 +153,17 @@ var params = OpenLayers.Util.getParameters(script.src); t.eq(params.k, "bar_param", "custom query string param"); - t.eq(params.cb_key, "cb_prefix:OpenLayers.Protocol.Script.registry['bar']", "callback with prefix"); + t.eq(params.cb_key, "cb_prefix:OpenLayers.Protocol.Script.registry.bar", "callback with prefix"); t.eq(script.id, 'OpenLayers_Protocol_Script_bar', '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'); + protocol.callbackTemplate = "customCallback(${id})"; + script = protocol.createRequest('http://bar_url/', {'k': 'bar_param2'}, 'bar_callback'); + + params = OpenLayers.Util.getParameters(script.src); + t.eq(params.k, "bar_param2", "custom query string param"); + t.eq(params.cb_key, "cb_prefix:customCallback(bar)", "custom callback with prefix"); OpenLayers.Protocol.Script.register = _register;