changeset 354:9330835535b9

fixed view double rendering
author cin
date Wed, 16 Oct 2013 17:28:40 +0400
parents feeb3bc4a818
children 8dfb9df07d02
files Lib/IMPL/Web/Handler/View.pm Lib/IMPL/Web/View/TTContext.pm Lib/IMPL/Web/View/TTView.pm
diffstat 3 files changed, 47 insertions(+), 35 deletions(-) [+]
line wrap: on
line diff
--- a/Lib/IMPL/Web/Handler/View.pm	Fri Oct 11 15:49:04 2013 +0400
+++ b/Lib/IMPL/Web/Handler/View.pm	Wed Oct 16 17:28:40 2013 +0400
@@ -60,12 +60,6 @@
         layout      => $this->layout
 	};
 
-    $this->view->display(
-      	$model,
-      	$template || $this->SelectView( $action, ref $model ),
-        $vars
-    );
-        
     my %responseParams = (
         type => $this->contentType,
         charset => $this->contentCharset,
--- a/Lib/IMPL/Web/View/TTContext.pm	Fri Oct 11 15:49:04 2013 +0400
+++ b/Lib/IMPL/Web/View/TTContext.pm	Wed Oct 16 17:28:40 2013 +0400
@@ -34,6 +34,7 @@
 	   includes
 	   modules
 	   aliases
+	   resolver
 	)) {
 		my $t = $prop;
 		
@@ -137,22 +138,23 @@
 	
 	my $prefix = $this->prefix;
 	
+	my $info;
+	
     if (not(($args and delete $args->{_no_resolve}) or ref $model)) {
-		$prefix = $prefix ? "${prefix}.${model}" : $model;
-		$model = $this->resolve_model($model);
+		$info = $this->resolve_model($model,$args);
 	} else {
-		$prefix = "";
+		$info = {
+			model => $model,
+			prefix => ""
+		};
 	}
 	
-	$template = $template ? $this->find_template($template) : $this->find_template_for($model);
+	$template = $template ? $this->find_template($template) : $this->find_template_for($info->{model});
 	
 	return $this->render(
         $template,
         hashApply(
-            {
-                prefix => $prefix,
-                model => $model,
-	        },
+            $info,
             $args
         )
     );
@@ -197,22 +199,24 @@
 	
 	$env ||= {};
 	
+	my $ctx = ($this->root || $this)->clone();
+
 	my $out = eval {
-		$this->localise(
+		$ctx->localise(
             hashApply(
 	            {
 	            	aliases => $this->aliases || {},
-					root => $this->root || $this,
+					root => $this->root || $ctx,
 					modules => $this->modules || {},
 					cache => TypeKeyedCollection->new(),
 		            display => sub {
-		                $this->display(@_);
+		                $ctx->display(@_);
 		            },
 		            render => sub {
-		            	$this->render(@_);
+		            	$ctx->render(@_);
 		            },
 		            display_model => sub {
-		            	$this->display_model(@_);
+		            	$ctx->display_model(@_);
 		            },
 		            tt_cache => {}
 				},
@@ -220,11 +224,11 @@
             )
         );
 		
-		&$code($this);
+		&$code($ctx);
 	};
 	
 	my $e = $@;
-	$this->delocalise();
+	$ctx->delocalise();
 	
 	die $e if $e;
     
@@ -279,16 +283,23 @@
 }
 
 sub resolve_model {
-	my ($this,$prefix) = @_;
+	my ($this,$prefix,$args) = @_;
 	
 	die ArgException->new(prefix => "the prefix must be specified")
 	   unless defined $prefix;
 	
 	#TODO handle DOM models
 	
+	if (my $resolver = $this->resolver) {
+		return $this->$resolver($prefix,$args);
+	}
+	
 	my @comp = map { $_, 0 } grep length($_), split(/\.|\[(\d+)\]/, $prefix);
 	
-	return $this->stash->get(['model',0,@comp]);
+	return {
+		model => $this->stash->get(['model',0,@comp]),
+		prefix => $this->prefix ? $this->prefix . ".$prefix" : $prefix
+	};
 }
 
 sub find_template_for {
--- a/Lib/IMPL/Web/View/TTView.pm	Fri Oct 11 15:49:04 2013 +0400
+++ b/Lib/IMPL/Web/View/TTView.pm	Wed Oct 16 17:28:40 2013 +0400
@@ -41,26 +41,33 @@
 			if ($layout) {
 				return $ctx->invoke_environment(
 					sub {
-						return $ctx->render($layout,$args);
+						return shift->render(
+							$layout,
+							hashMerge(
+								$args,
+								{
+									content => sub {
+										$ctx->invoke_environment(
+											sub {
+												return shift->display_model($model,$template,$args);
+											},
+											{
+												base => $this->viewBase
+											}
+										)
+									}
+								}
+							)
+						); # render
 					},
 					{
 						base => $this->layoutBase,
-						content => sub {
-							$ctx->invoke_environment(
-								sub {
-									return shift->display_model($model,$template,$args);
-								},
-								{
-									base => $this->viewBase
-								}
-							)
-						}
 					}
 				);
 			} else {
 				return $ctx->invoke_environment(
 					sub {
-						return $ctx->display_model($model,$template,$args);
+						return shift->display_model($model,$template,$args);
 					},
 					{
 						base => $this->viewBase