FW/1 ColdSpring Services That Rely On The Prior

Posted: March 13, 2012 in ColdFusion

In FW/1 you have the variables.fw.service method to work with in your controllers. This method will run your service’s method and return the results to a key inside the RC scope. Keep in mind, however, these service calls are queued up and not run until the view is called. Why this is done I have no idea, but I’m sure there’s a reason.

Because they are queued, something like this would return a variable not defined error:

variables.fw.service ( 'users.get', 'userRecord' );
variables.fw.service ( 'geographic.getStatesByCountry', 'states', { userID = userRecord.getUserID() );

So after spending some time reading through threads on Google Groups, I determined the old fashioned way is the best way. However there's a nice feature if you have a bean service defined such as ColdSpring. This feature implicitly sets a property named the same as a defined bean in your XML configuration. For example:

component accessors = true {
  property usersService;

  function init ( FW ) {
    variables.fw = arguments.fw;
    return this;
  }

  void function default ( RC ) {
    RC.performerRecord = getUsersService().get ( ID = 1 );
    variables.fw.service ( 'geographic.getStatesByCountry', 'states', { userID = userRecord.getUserID() );
  }
}

You don't need use the fw.service on the last call, but I left it in there ... just because.

What happens is FW/1 will look to see if each property defined matches a bean definition. If found it automatically sets the property to the bean. Otherwise you'd normally put something like this into your init:

setUsersService ( application.beanfactory.getBean ( "users" ) );
About these ads
Comments
  1. Spills says:

    You have incorrect information regarding the lifecycle for FW/1, “… these service calls are queued up and not run until the view is called.”, they are run before any views/layouts are rendered you can view https://github.com/downloads/seancorfield/fw1/fw1.pdf for more detail on the lifecycle execution (eventhough the document is a little old). You can always call your services directly if you need, but you have access to any returned data in the endItem() and after() methods in your controller and of course setupRequest() and setupView() all which happen before any views or layouts are called.

    Spills

  2. Chris says:

    Thanks for the comment Spills. I was generalizing on when the services are called. However using endItem() and after() are still pretty worthless still if you are dealing with more than two service calls.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s