Re: [Maypole] Re: Maypole responding to requests very slowly (or, how to get just one associated record instead of getting the world)

From: Brian Glass (brian at glassbrian.com)
Date: Sun Nov 28 2004 - 01:41:45 GMT


Dave Howorth wrote:

>> I only use AsForm for non-relationship fields. It turned out that the
>> real performance problems were created by object creation, not large
>> queries or MySQL. This little tweak makes my Maypole application
>> very fast.
>
>
> Hi Brian,
>
> Your function looks very interesting to me. I'd love to see your
> template.
>
> Cheers, Dave
>
> _______________________________________________
> maypole mailing list
> maypole at lists.netthink.co.uk
> http://lists.netthink.co.uk/listinfo/maypole
>
Minimally, you pass an object or class and the field name ala:

<& macros/to_field, object => $family, field => 'persons' &>

There are other arguments as well. Assuming the $family object has_many
persons this will produce a select list of persons. It should work for
just about any relationship. Note also that you can overide the
simple_list method in your subclasses if you need to do something fancier.

If people are interested or have ideas for improvements or better ways
to do this I can flesh it out a little better. I would think this sort
of thing would come in handy in the factory templates.

<%doc>
Convert an object/class + field to the appropriate HTML form field.
</%doc>

%if( $meta ){
    <select id="<% $field %>" name="<% $field %>" onchange="<% $onchange
| h %>">
        <%perl>
        for my $item ( @{$listClass->simple_list} ){
            my $value = substr( $item->[1], 0, $maxSelect );
            $value .= '...' if length($item->[1]) > length($value);

            if( $item->[0] eq $current ){
            </%perl>
                <option value="<% $item->[0] %>" selected="1"><% $value
%></option>
% }else{
                <option value="<% $item->[0] %>"><% $value %></option>
% }
% }
    </select>
%}elsif( $values ){
    <select id="<% $field %>" name="<% $field %>" onchange="<% $onchange
%>">
% for my $value ( @$values ){
% if( $current eq $value ){
                <option value="<% $value %>" selected="1"><% $value
%></option>
% }else{
                <option value="<% $value %>"><% $value %></option>
% }
% }
    </select>
%}else{
    <% $html->as_HTML %>
%}

<%init>
my ($meta, $name, $listClass, $html);

if( $meta = $object->meta_info( has_many => $field ) ){
    if( $name = $meta->{args}->{mapping}->[0] ){
        $listClass = $meta->{foreign_class}->meta_info( has_a => $name
)->{foreign_class};
    }else{
        $name = $meta->{accessor};
        $listClass = $meta->{foreign_class};
    }
}elsif( $meta = $object->meta_info( has_a => $field ) ){
    $name = $meta->{accessor}->{name};
    $listClass = $meta->{foreign_class};
    my $id = $object->primary_column;
    $current = $object->$field->$id if ref($object) and $object->$field;
}elsif( $values ){
    $current = $object->$field if ref($object);
}else{ # here we use AsForm
    $html = $object->to_field($field);
    $html->attr( value => $current ) if $current;
    while( my ($attr, $value) = each(%ARGS) ){
        $html->attr( $attr => $value ) unless $attr =~
/(object|field|maxSelect)/;
    }
}
</%init>

<%args>
$object
$field
$maxSelect => 25
$values => undef
$onchange => undef
$current => undef
</%args>

_______________________________________________
maypole mailing list
maypole at lists.netthink.co.uk
http://lists.netthink.co.uk/listinfo/maypole



This archive was generated by hypermail 2.1.3 : Thu Feb 24 2005 - 22:25:57 GMT