Ray
Ray

Reputation: 6095

grails ajax functionality - an "annoying bug"

How does one use remoteLink or remoteFunction to invoke an action, and then use what that action produces/returns inside a JavaScript function? As soon as I put a parameter inside the JavaScript function to call on completion of the action, the function is no longer invoked at all, e.g.:

    onComplete: 'processUpdate(e);'    --or--

    after: 'processAfter(e)'

then, the Javascript function is no longer called at all. Fyi: whole line of code is:

<g:select name="week" from="${strategyInstance.constraints.week.inList}" value="${strategyInstance?.week}" valueMessagePrefix="strategy.week" noSelection="['': '']"
   onchange="${remoteFunction(action: 'produceUpdate', params: '\'update=\' + this.value', onComplete:'processUpdate(e);', before:  'alert("doing before")', after: 'processAfter(e)')}"/> 

If I remove the parameter "e" out of the Javascript function name, i.e.:

 onComplete: 'processUpdate();'

then it calls the function. If I declare the function:

  function processUpdate(e) {
   alert('processUpdate: ' +e)
  }

it invokes the function but alerts me with:

     processUpdate: undefined

So, how does one with Grails get results produced by actions into a Javascript function that one can then work on/with?

Upvotes: 0

Views: 1428

Answers (1)

Chris
Chris

Reputation: 8109

There is a very annoying bug in xml-parsing mechanism: As soon as you define an attribute within a grails-tag, which contains double-quotes, and the tag itself uses double quotes, the complete attribute definition is ignored. Your alert uses double quotes. Try:

<g:select name="week" from="${strategyInstance.constraints.week.inList}" 
  value="${strategyInstance?.week}" 
  valueMessagePrefix="strategy.week" 
  noSelection="['': '']"
  onchange="${remoteFunction(
      action: 'produceUpdate', 
      params: '\'update=\' + this.value', 
      onComplete:'processUpdate(e);', 
      before:  'alert(\'doing before\')', 
      after: 'processAfter(e)')}"/>

onComplete is called independent of whether the AJAX call is fine or not (so success and failure). If you want to access the result in success case use onSuccess with parameter data:

<g:select name="week" from="${strategyInstance.constraints.week.inList}" 
  value="${strategyInstance?.week}" 
  valueMessagePrefix="strategy.week" 
  noSelection="['': '']"
  onchange="${remoteFunction(
      action: 'produceUpdate', 
      params: '\'update=\' + this.value', 
      onSuccess: 'processUpdate(data);', 
      before:  'alert(\'doing before\')', 
      after: 'processAfter(e)')}"/>

Upvotes: 1

Related Questions