[Jifty-commit] r1071 - in jifty/trunk: lib/Jifty lib/Jifty/Web

jifty-commit at lists.jifty.org jifty-commit at lists.jifty.org
Tue May 16 13:27:39 EDT 2006


Author: alexmv
Date: Tue May 16 13:27:37 2006
New Revision: 1071

Modified:
   jifty/trunk/   (props changed)
   jifty/trunk/lib/Jifty/Web.pm
   jifty/trunk/lib/Jifty/Web/PageRegion.pm

Log:
 r13031 at zoq-fot-pik:  chmrr | 2006-05-16 13:27:30 -0400
  * ->render on page regions now calls ->enter, outputs via Jifty->out,
    calls ->exit, and returns an empty string -- this breaks backwards
    compatibility.  The previous functionality is accessible via
    ->as_string
 
  * Revert r1069 -- use the more sensible $region->render for this
 
  * Regions now establish their qualified names and parents at creation
    time, which means they can be referred to before they are rendered
    now.  This was not previously the case because I was imagining
    pathological cases where a region was created in one region, and
    passed to inside another region before being rendered.  However,
    this is impossible, as references cannot pass region boundries.
    This removes the cumbersome restriction that qualified names be
    established only at render-time.


Modified: jifty/trunk/lib/Jifty/Web.pm
==============================================================================
--- jifty/trunk/lib/Jifty/Web.pm	(original)
+++ jifty/trunk/lib/Jifty/Web.pm	Tue May 16 13:27:37 2006
@@ -914,40 +914,23 @@
     return $self->{'regions'}{$name};
 }
 
-=head3 region (PARAMHASH or REGION)
+=head3 region PARAMHASH
 
-If passed an even number of arguments as a C<PARAMHASH>, the PARAMHASH
-is used to created and renders a L<Jifty::Web::PageRegion>; the
-C<PARAMHASH> is passed directly to its L<Jifty::Web::PageRegion/new>
-method.  Otherwise, it acts on the given C<REGION>.
-
-However it procures the region, it is
-L<Jifty::Web::PageRegion/enter>ed, then
-L<Jifty::Web::PageRegion/render>ed, and finally
-L<Jifty::Web::PageRegion/exit>ed.
+The provided PARAMHASH is used to create and render a
+L<Jifty::Web::PageRegion>; the C<PARAMHASH> is passed directly to its
+L<Jifty::Web::PageRegion/new> method, and then
+L<Jifty::Web::PageRegion/render> is called.
 
 =cut
 
 sub region {
     my $self = shift;
 
-    my $region;
-    if (@_ % 2) {
-        $region = shift;
-    } else {
-        $region = Jifty::Web::PageRegion->new(@_) or return; 
-    }
-
-    # Enter the region
-    $region->enter;
+    # Create a region
+    my $region = Jifty::Web::PageRegion->new(@_) or return; 
 
     # Render it
-    $self->out( $region->render );
-
-    # Exit it when we're done
-    $region->exit;
-    
-    "";
+    $region->render;
 }
 
 =head3 current_region
@@ -964,16 +947,18 @@
         : undef;
 }
 
-=head3 qualified_region
+=head3 qualified_region [REGION]
 
 Returns the fully qualified name of the current
-L<Jifty::Web::PageRegion>, or the empty string if there is none..
+L<Jifty::Web::PageRegion>, or the empty string if there is none.  If
+C<REGION> is supplied, gives the qualified name of C<REGION> were it
+placed in the current region.
 
 =cut
 
 sub qualified_region {
     my $self = shift;
-    return join( "-", map { $_->name } @{ $self->{'region_stack'} || [] } );
+    return join( "-", map { $_->name } @{ $self->{'region_stack'} || [] }, @_ );
 }
 
 =head3 serve_fragments
@@ -1013,7 +998,7 @@
 
         # Stuff the rendered region into the XML
         $writer->startTag( "fragment", id => Jifty->web->current_region->qualified_name );
-        $writer->cdata( Jifty->web->current_region->render );
+        $writer->cdata( Jifty->web->current_region->as_string );
         $writer->endTag();
 
         Jifty->web->current_region->exit while Jifty->web->current_region;

Modified: jifty/trunk/lib/Jifty/Web/PageRegion.pm
==============================================================================
--- jifty/trunk/lib/Jifty/Web/PageRegion.pm	(original)
+++ jifty/trunk/lib/Jifty/Web/PageRegion.pm	Tue May 16 13:27:37 2006
@@ -81,12 +81,18 @@
     }
 
     $self->name($args{name});
+    $self->qualified_name(Jifty->web->qualified_region($self));
     $self->default_path($args{path});
     $self->default_arguments($args{defaults});
     $self->arguments({});
-    $self->parent($args{parent});
+    $self->parent($args{parent} || Jifty->web->current_region);
     $self->region_wrapper(not $args{_bootstrap} and $args{region_wrapper});
 
+    # Keep track of the fully qualified name (which should be unique)
+    $self->log->warn("Repeated region: " . $self->qualified_name)
+        if Jifty->web->get_region( $self->qualified_name );
+    Jifty->web->{'regions'}{ $self->qualified_name } = $self;
+
     return $self;
 }
 
@@ -180,17 +186,8 @@
 sub enter {
     my $self = shift;
 
-    $self->log->warn("Region occurred in more than once place: ".$self->qualified_name)
-      if (defined $self->parent and $self->parent != Jifty->web->current_region);
-
-    $self->parent(Jifty->web->current_region);
+    # Add ourselves to the region stack
     push @{Jifty->web->{'region_stack'}}, $self;
-    $self->qualified_name(Jifty->web->qualified_region);
-
-    # Keep track of the fully qualified name (which should be unique)
-    $self->log->warn("Repeated region: " . $self->qualified_name)
-        if Jifty->web->get_region( $self->qualified_name ) and Jifty->web->get_region( $self->qualified_name ) ne $self;
-    Jifty->web->{'regions'}{ $self->qualified_name } = $self;
 
     # Merge in the settings passed in via state variables
     for (Jifty->web->request->state_variables) {
@@ -223,31 +220,33 @@
     }
 }
 
-=head2 render
+=head2 as_string
 
-Returns a string of the fragment and associated javascript.
+Deals with the bulk of the effort to show a page region.  Returns a
+string of the fragment and associated javascript (if any).
 
 =cut
 
-sub render {
+sub as_string {
     my $self = shift;
 
+    if (Jifty->web->current_region ne $self) {
+        # XXX TODO: Possibly we should just call ->enter
+        warn "Attempt to call as_string on a region which is not the current region";
+        return "";
+    }
+    
     my %arguments = %{ $self->arguments };
 
     # undef arguments cause warnings. We hatesses the warnings, we do.
     defined $arguments{$_} or delete $arguments{$_} for keys %arguments;
     my $result = "";
 
-    # We need to tell the browser this is a region and
-    # what its default arguments are as well as the path of the "fragment".
-
-    # We do this by passing in a snippet of javascript which encodes this
-    # information.
-
-# Alex is sad about: Anything which is replaced _must_ start life as a fragment.
-# We don't have a good answer for this yet.
-
-# We only render the region wrapper if we're asked to (which is true by default)
+    # We need to tell the browser this is a region and what its
+    # default arguments are as well as the path of the "fragment".  We
+    # do this by passing in a snippet of javascript which encodes this
+    # information.  We only render this region wrapper if we're asked
+    # to (which is true by default)
     if ( $self->region_wrapper ) {
         $result .= qq|<script type="text/javascript">\n|
             . qq|new Region('| . $self->qualified_name . qq|',|
@@ -285,12 +284,27 @@
     $result .= $region_content;
     $result .= qq|</div>| if ( $self->region_wrapper );
 
-
     Jifty->handler->mason->interp->out_method( \&Jifty::View::Mason::Handler::out_method );
 
     return $result;
 }
 
+=head2 render
+
+Calls L</enter>, outputs the results of L</as_string>, and then calls
+L</exit>.  Returns an empty string.
+
+=cut
+
+sub render {
+    my $self = shift;
+
+    $self->enter;
+    Jifty->web->out($self->as_string);
+    $self->exit;
+    "";
+}
+
 =head2 get_element [RULES]
 
 Returns a CSS2 selector which selects only elements under this region


More information about the Jifty-commit mailing list