Today a friend asked me to look over some code he wrote using an MVC framework. One of the first things I noticed was that there was just to much logic going on in some of the views. I would see code that was directing the behavior of the view which is a big no no, we will get to that in a second. In the edit forms for a product I saw logic like this.
You may not realize it but by doing this your actually breaking one of the key concepts of MVC. The view should only concern itself with presentation and it should always look to delegate to the controller for its logic. The reason we do this is to keep modularity by maintaining loose coupling with the controller. A better way of handling the issue above would be to delegate this logic to our controller.
I guess the main point of this entry is to remind us (I make the same mistake) that just because we use a framework doesn't mean your work is done. You still need to be mindful of why you are using it in the first place!

#1 by Raymond Camden on 1/11/10 - 6:16 AM
Now, if you want to get anal about it, I could see adding code to your model: amINew() (horrible method name). Your view could then do:
<cfif product.amINew()>
<cfset title = "Add...">
<cfelse>
<cfset title = "Edit...">
</cfif>
#2 by Brian Swartzfager on 1/11/10 - 7:06 AM
#3 by Dan Vega on 1/11/10 - 7:25 AM
#4 by Raymond Camden on 1/11/10 - 7:41 AM
#5 by Brian Swartzfager on 1/11/10 - 8:22 AM
So (to use your example) say you're asked to write another application that uses these Product objects for some other purpose. Your clients for this new application also want to be able to add new products, but they don't want the page title to be "Add New Product" (they're being nitpicky and want something else). That means you'd end up either needing to change your edit() function or (even worse) making a new edit() function in your model just to accomodate this different view.
And if the new application didn't even allow users to add new products (maybe the app is simply a read-only interface that lists products), you'd have this page title code in your model that would simply be taking up space/not being utilized.
#6 by Sam Farmer on 1/11/10 - 9:51 AM
#7 by Adam Haskell on 1/11/10 - 3:14 PM
I do disagree with Ray on the analness of isNew, nothing but the model should have logic to know if it is new or not(unless the lack of an object means it is new <cfif controller.getObject() EQ "" />). Views visually represent the state of object(s). Views talk directly to Objects to interrogate and figure out how to represent the state of the object.
#8 by Raymond Camden on 1/11/10 - 3:18 PM
#9 by Adam Haskell on 1/11/10 - 3:28 PM