Skip to content

Commit 8543e46

Browse files
author
graeme
committed
fix for GRAILS-2512
git-svn-id: https://svn.codehaus.org/grails/trunk@6802 1cfb16fd-6d17-0410-8ff1-b7e8e1e2867d
1 parent 952d457 commit 8543e46

File tree

2 files changed

+64
-13
lines changed

2 files changed

+64
-13
lines changed

src/groovy/org/codehaus/groovy/grails/plugins/web/taglib/JavascriptTagLib.groovy

Lines changed: 55 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -192,9 +192,10 @@ class JavascriptTagLib {
192192
if(!value) value = ''
193193

194194
out << "<input type=\"text\" name=\"${attrs.remove('name')}\" value=\"${value}\" onkeyup=\""
195-
if(attrs.params) {
195+
196+
if(attrs.params) {
196197
if(attrs.params instanceof Map) {
197-
attrs.params.put(paramName, 'this.value')
198+
attrs.params.put(paramName, new JavascriptValue('this.value'))
198199
}
199200
else {
200201
attrs.params += "+'${paramName}='+this.value"
@@ -332,6 +333,19 @@ interface JavascriptProvider {
332333
def prepareAjaxForm(attrs)
333334
}
334335

336+
class JavascriptValue {
337+
def value
338+
339+
public JavascriptValue(value) {
340+
this.value = value
341+
}
342+
343+
public String toString() {
344+
return "'+$value+'"
345+
}
346+
347+
348+
}
335349
/**
336350
* Prototype implementation of JavaScript provider
337351
*
@@ -366,21 +380,32 @@ class PrototypeProvider implements JavascriptProvider {
366380

367381
//def pms = attrs.remove('params')
368382
def url
383+
def jsParams = attrs.params?.findAll { it.value instanceof JavascriptValue }
384+
385+
jsParams?.each { attrs.params?.remove(it.key) }
386+
387+
388+
369389
if(attrs.url) {
370390
url = taglib.createLink(attrs.url)
371391
}
372392
else {
373393
url = taglib.createLink(attrs)
374394
}
395+
396+
if(!attrs.params) attrs.params = [:]
397+
jsParams?.each { attrs.params[it.key] = it.value }
398+
399+
375400
def i = url?.indexOf('?')
376401

377402
if(i >-1) {
378403
if(attrs.params instanceof String) {
379404
attrs.params += "+'&${url[i+1..-1].encodeAsJavaScript()}'"
380405
}
381406
else if(attrs.params instanceof Map) {
382-
def params = attrs.params.collect { k, v -> "${k.encodeAsURL()}=${v.encodeAsURL()}" }.join('&').encodeAsJavaScript()
383-
attrs.params = "'$params&${url[i+1..-1].encodeAsJavaScript()}'"
407+
def params = createQueryString(attrs.params)
408+
attrs.params = "'${params}${params ? '&' : ''}${url[i+1..-1].encodeAsJavaScript()}'"
384409
}
385410
else {
386411
attrs.params = "'${url[i+1..-1].encodeAsJavaScript()}'"
@@ -399,9 +424,30 @@ class PrototypeProvider implements JavascriptProvider {
399424
// process options
400425
out << getAjaxOptions(attrs)
401426
// close
402-
out << ');'
403-
}
404-
427+
out << ');'
428+
attrs.remove('params')
429+
}
430+
431+
private String createQueryString(params) {
432+
def allParams = []
433+
for (entry in params) {
434+
def value = entry.value
435+
def key = entry.key
436+
if (value instanceof JavascriptValue) {
437+
allParams << "${key.encodeAsURL()}='+${value.value}+'"
438+
}
439+
else {
440+
allParams << "${key.encodeAsURL()}=${value.encodeAsURL()}".encodeAsJavascript()
441+
}
442+
}
443+
if(allParams.size() == 1) {
444+
return allParams[0]
445+
}
446+
else {
447+
return allParams.join('&')
448+
}
449+
}
450+
405451
// helper function to build ajax options
406452
def getAjaxOptions(options) {
407453
def ajaxOptions = []
@@ -432,13 +478,9 @@ class PrototypeProvider implements JavascriptProvider {
432478
if(options.params) {
433479
def params = options.remove('params')
434480
if (params instanceof Map) {
435-
ajaxOptions << "parameters:'" +
436-
params.collect { k, v -> "${k.encodeAsURL()}=${v.encodeAsURL()}" }.join('&').encodeAsJavaScript() +
437-
"'"
438-
}
439-
else {
440-
ajaxOptions << "parameters:${params}"
481+
params = createQueryString(params)
441482
}
483+
ajaxOptions << "parameters:${params}"
442484
}
443485
}
444486
// remaining options

test/groovy/org/codehaus/groovy/grails/web/taglib/JavaScriptTagLibTests.groovy

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,15 @@ class TestUrlMappings {
1919
grailsApplication.addArtefact(UrlMappingsArtefactHandler.TYPE, urlMappingsClass)
2020
}
2121

22+
void testRemoteFieldWithExtraParams() {
23+
def template = '<g:remoteField controller="test" action="hello" id="1" params="[var1: \'one\', var2: \'two\']" update="success" name="myname" value="myvalue"/>'
24+
25+
request.setAttribute("org.codehaus.grails.INCLUDED_JS_LIBRARIES", ['prototype'])
26+
27+
assertOutputEquals '<input type="text" name="myname" value="myvalue" onkeyup="new Ajax.Updater(\'success\',\'/test/hello/1\',{asynchronous:true,evalScripts:true,parameters:\'value=\'+this.value+\'&var1=one&var2=two\'});" />', template
28+
29+
}
30+
2231
void testPrototypeSubmitToRemoteWithExtraParams() {
2332
def template = '<g:submitToRemote name="myButton" url="[controller:\'person\', action:\'show\', params:[var1:\'one\', var2:\'two\']]" ></g:submitToRemote>'
2433
request.setAttribute("org.codehaus.grails.INCLUDED_JS_LIBRARIES", ['prototype'])

0 commit comments

Comments
 (0)