ClearPress Framework

Views

Views inherit and extend functionality of the ClearPress::view superclass. At the very minimum, application views can contain as little as no code, just the reference to their superclass for inheritance purposes. package Grandkids::view::kid; use strict; use warnings; use base qw(ClearPress::view); 1;

The superclass defines all of the basic functionality to support extended CRUD mechanisms (create, read, update, delete, list, edit, add) for XML, JSON, HTML and AJAX/HTML fragments.

Whilst basic functionality gets you part-way there it's often not quite enough for more complicated scenarios. To take care of these situations any method implemented in the view with the right systematic name will override the method of the same name in the superclass, e.g. list, list_xml, read, read_png, create_json etc.

Within these methods it is possible to access all of the useful bits for processing a request and buildig a response.

Accessing CGI

When processing a web request it's usually handy to have any CGI parameters. To get hold of them grab the instance of CGI.pm: sub list { my $self = shift; my $util = $self->util; my $cgi = $util->cgi; ... }

Accessing the data model

Almost all requests need to do something with one or more data models so at the very least you'll need to locate the one corresponding to this view method before you do something with it: sub list { my $self = shift; my $model = $self->model; ...

Putting it together

Let's say you have a small cascade of related objects, let's say a family with a child, which need to be built from the results of a POSTed form and saved together in one transaction. Assuming we're in Grandkids::view::family... sub create { my $self = shift; my $util = $self->util; my $cgi = $util->cgi; my $family = $self->model; my $f_name = $cgi->param('name'); $family->name($f_name); my $c_name = $cgi->param('child_name'); my $child = Grandkids::model::child->new({name => $c_name}); $family->children($child); return $family->create(); } This makes quite a lot of other assumptions too - the form posting to this code has to contain both name and child_name fields, and only one of each and the family model's create method needs to check for children to save as part of its transaction.