3

I am using ColdBox with ColdFusion 10. I wanted to pass an argument say id=1000 with the setView(). I couldn't find any example where a param is being passed.

Here is the code:

component {
// Dependency Injection
property name="requestService" inject="RequestService";

function index(event, rc, prc) {        
    var response = requestService.save(rc);

    if(response.Success EQ true) {
        event.setView(view="requests/success"); //Want to pass a param(int)
    } else {
        event.setView("requests/failure");
    }
  }
}
Kindle G.
  • 51
  • 7

2 Answers2

5

There are two main ways to pass values from your handler to your view.

The first is to place the values in the Private Request Collection which is made available in handlers as a struct called "prc". The view has the same "prc" struct available to it. This request collection is available to the entire request and all layouts or views that execute for that request.

In your handler

prc.id = 1000;
event.setView( view="requests/success" );

In your view

<cfoutput>#prc.id#</cfoutput>

If you want a more encapsulated approach that only makes the value available to that view specifically, you can use the "args" parameter to the event.setView() and pass a struct of values that will be made available in the view in a struct called "args".

In your handler

event.setView( view="requests/success", args={ id = 1000 } );

In your view

<cfoutput>#args.id#</cfoutput>
Brad Wood
  • 3,863
  • 16
  • 23
1

Just set it into the PRC.

prc.foo = 1000

When you set a View, you're telling the layout what View to render. The View can reference any RC or PRC variable that's been defined before it was set. Same with renderView() and Viewlets, just define a variable before it or define some args (struct) as an argument.

#renderView(view='forms/universal',args={type='new',action='user.create'})#

Now, if you were forwarding to another event, you'd have to persist any values that you want to exist in the next event.

Adrian J. Moreno
  • 14,350
  • 1
  • 37
  • 44