[Catalyst] MoreFewer
Vsevolod (Simon) Ilyushchenko
simonf at cshl.edu
Mon Apr 24 14:09:23 CEST 2006
Hi,
Here's version 0.01 of Catalyst::Plugin::MoreFewer.
http://www.simonf.com/catalyst/Catalyst-Plugin-MoreFewer-0.01.tar.gz
Before I release it to CPAN, I would appreciate if more skilled people
than I could look over the code and the example and tell me if either
needs improvement.
This component helps your controller and forms deal with multiple rows
of child fields (eg, a book can have many authors and many pages, and
you might need to edit them all on one form). Specifically, it's very
easy to have the buttons 'More' and 'Fewer' on the form to add or remove
children without saving the data to the database.
The example is in t/lib/Recipe. It shows a list of dishes. You can run
the test server using t/lib/scripts/recipe_server.pl. It will create a
sqlite database in /tmp/recipe.db. The dish edit window has three kinds
of children:
1. cooks - one-to-many table, children are deleted and recreated on
every save,
2. food (=ingredients) - many-to-many table, children are deleted and
recreated on every save, and
3. steps - one-to-many table that has to preserve child IDs, so they are
NOT deleted on save.
The HTML template that uses More and Fewer buttons is
t/lib/root/recipe_modify.html.
Mst said on IRC that it would be good to split the component into the
controller- and view- related parts, but I'm not quite sure how to do
it. At least that remark prompted me to create a separate
Catalyst::Model::ChildModel class (used internally) and simplify the API.
I'm also going to create Helpers that generate scaffolding code, but
first I'd like to be sure I'm doing the right thing.
Some specific questions:
1. Are the names Catalyst::Plugin::MoreFewer and
Catalyst::Model::ChildModel appropriate?
2. The sample application lives in t/lib as a separate Perl module
distribution. It's a bit weird, but I see no other way, as it should not
be a part of the main distribution.
3. Please criticize the approach I used in Recipe::Controller::Dish. The
controller has to be able to return the current $c object and to tell
MoreFewer if it's in data saving mode via the creating_data() and
saving_data() methods. Hence, it has instance variables qw(c op class
model), which are initialized in auto(). ('Class' and 'model' are there
to make it look a little more generic.) In order to help auto() get the
operation and class names from the URL, I have to use Regex actions
instead of something simpler. I follow this approach in my other
applications, and if there is a simpler way to achieve the same result,
I sure would like to hear it.
4. Are there any other ways of dealing with children that I've missed?
Thanks,
Simon
--
Simon (Vsevolod ILyushchenko) simonf at cshl.edu
http://www.simonf.com
"Think like a man of action, act like a man of thought."
Henri Bergson
More information about the Catalyst
mailing list