1

I'm learning the FW/1 framework and so far so good. I understand mostly how controllers/services/views work. However I have some additional functions that manipulate the views in some cases i.e. alter the CSS and layout depending on what is returned to the view. Where is the best place to add this functions to make them accessible by the view?

Jeromy French
  • 11,812
  • 19
  • 76
  • 129
Shaun Perry
  • 159
  • 1
  • 2
  • 12

1 Answers1

3

To use an example, I typically will use a 'formatter' object - for formatting dates, etc. consistently in my applications. To accomplish this in a FW/1 app I have typically have a controller method I call in setUpRequest() that will put the formatter object into the request context (rc).

For example, My setupRequest() method may look like this

function setupRequest( rc ) {
    controller( 'setup.default' );
}

And in setup.default() I would have code similar to this:

component accessors="true" {

    property Any formatter;

    public void function default( Any rc ){
        rc.formatter = formatter;
    }
}

I use ColdSpring to handle my dependency injection - but I am pretty sure you can just as easily use DI/1 and not have any of this code change at all.

Then, for example, if I need to format a date in a view, I simply use this:

rc.formatter.formatDate( someObject.getSomeDate() )

You could modify this example to use different logic for your CSS, etc., put that logic into a CFC and include it in the request context (rc).

Seybsen
  • 14,989
  • 4
  • 40
  • 73
Scott Stroz
  • 7,510
  • 2
  • 21
  • 25
  • Thanks for taking the time to explain this. This all makes sense. This works well for standard functions that can be called all over the application, however is it still best to do this way if the functions are business logic, that e.g. say I have a function that takes ids outputted on the view then does some more database querying which then alters the output in the view? – Shaun Perry Jul 19 '13 at 13:06
  • Ideally, you want to preform those other data base queries and use that data in the view to determine what gets output. If you can give more details, it might be helpful in guiding you down the right path. – Scott Stroz Jul 19 '13 at 14:42