Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

gav (2710)

gav
  (email not shown publicly)
http://www.estey.com/
AOL IM: flufflegavin (Add Buddy, Send Message)

Hacker in NYC.

Journal of gav (2710)

Sunday January 04, 2004
04:31 PM

state in web apps

[ #16627 ]

Recently I've been thinking about how to improve the handling of state in the web applications I write. I had the idea to store the states in a HoH:

state    | prev     | next     | run          | pre_cond

cart     | undef    | shipping | show_cart    | undef
shipping | cart     | billing  | get_ship     | is_valid_cart
billing  | shipping | summary  | get_bill     | is_valid_ship
summary  | billing  | undef    | show_summary | is_valid_bill

"run" and "pre_cond" are method names to call, where "run" methods show the next form and "pre_cond" are predicates to determine if we can call this method. I've included the "next" state in this table so it can be passed to the "run" method so as not to hard code it.

This is a change from how I've been previously doing it, normally I'd just have a hash to look up from state name to a sub ref to call. The idea is now I can make sure that all the previous states are valid before showing the next. This solves the problem where we worry about a user fiddling with the flow by changing the state in the HTML. It also means that we can take into account things external to the current flow, for example this means that a user can bookmark a specific page and return to it, but if any of the previous states are now invalid (e.g. a product is now out of stock) we can return to that state without blindly continuing (or having to repeat the code to do the test twice).

It's something I've been thinking about for the last week. Is this something that makes sense? How do you handle state in your web applications?

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.