holgrich
holgrich

Reputation: 361

Render Grails template depending on JS variable

situation is: I have a javascript bool variable named status. If it is true, I want to render a Grails template. If it is false, I want to render another Grails template. My code:

HTML/JS

<div id="content">
<g:javascript>
    $( function(){
        if( status==true ) {
            $.ajax({
                url: '/tool/home/renderSplash',
                type: 'POST',
                failure: function(){alert("Failed loading content frame"})
            }
        else{
            $.ajax({
                url: '/tool/home/renderContent',
                type: 'POST',
                failure: function(){alert("Failed loading content frame")}
            })
        }
    } )
</g:javascript>
</div>

Grails:

class HomeController {

  def index() {}

  def renderSplash() {
      render( template: "splash" )
  }

  def renderContent() {
      render( template: "content" )
  }
}

I can see, that POST contains the correct template data, but its not brought to the screen.

Am I doing it the correct way?

Upvotes: 1

Views: 2209

Answers (1)

ataylor
ataylor

Reputation: 66069

Add a success handler to your AJAX call that renders the result. You'll need a placeholder element to render it on. Example:

$.ajax({
       url: '/tool/home/renderContent',
       type: 'POST',
       failure: function(){alert("Failed loading content frame")},
       success: function(response) { $('#response').html(response); }
   })

...
<div id='response'></div>

Upvotes: 5

Related Questions