views:

863

answers:

3
+1  Q: 

Grails web flow

Hi,

Is there any way to pass model data to a view state? Consider the following example view state:

class BookController {
  def shoppingCartFlow = {
    showProducts {
      on("checkout").to "enterPersonalDetails"
      on("continueShopping").to "displayCatalogue"
    }
  }
}

If I want to pass the data model [products: Product.list()] to showProducts.gsp, is there any way to do this apart from preceding the view state with an action state that stores the model in flow scope?

Thanks, Don

A: 

Maybe I don't understand the question, but can't you do

render (view:"showProducts", model:[products: Product.list()]

inside your controller?

Jean Barmash
This is not a normal controller action, it's a web-flow state. I'm not sure the render method can be used as you've suggested within view states.
Don
A: 

You can try this (assuming you want go to checkout):

showProducts {
      on("checkout"){
           // do somethings here too if you like
           // then pass your data as below:
           [products: Product.list()]
      } .to "enterPersonalDetails"
      on("continueShopping").to "displayCatalogue"
}
tegbains
I want to pass the data to showProducts.gsp, not enterPersonalDetails.gsp
Don
I was trying to give you an example so you can build on that...
tegbains
+1  A: 

Hmm, it's been a bit since I did a flow, and your example is simplistic (just for being an example's sake, I hope).

What your missing is the initial action in the flow. Keep in mind that a "view" flow action as your showProducts is just says what to do when your showProducts gsp POSTS. It's the action that SENT you to showProducts that should create the model to be used in showProducts.gsp

def ShoppingCartFlow = {
   initialize {
       action {  // note this is an ACTION flow task
           // perform some code
           [ model: modelInstance ] // this model will be used in showProducts.gsp
       }
       on ("success").to "showProducts"      
       // it's the above line that sends you to showProducts.gsp
   }

   showProducts {
        // note lack of action{} means this is a VIEW flow task
        // you'll get here when you click an action button from showProducts.gsp
      on("checkout").to "enterPersonalDetails"
      on("continueShopping").to "displayCatalogue"
   }

   // etc. (you'll need an enterPersonalDetails task, 
   // displayCatalogue task, and they
   // should both be ACTION tasks)
}

Make sense?

Bill James
That's exactly how I implemented it, by using 2 stated: an action state followed by a view state. My question was really whether these could be merged into a single state. So I guess the answer is no.
Don
Right, the only time you can have code (including creating a model) is in an action task or a "transition" task (between the on("") and the .to"" in view tasks)
Bill James