changeset 190:cd1ff7029a63

IMLP::Web::View refactored, added new method 'require' which is available inside templates. Changed document rendering.
author cin
date Wed, 04 Apr 2012 17:51:27 +0400
parents 08015e2803f1
children 78a18a2b6266
files Lib/IMPL/Profiler/Memory.pm Lib/IMPL/Web/View/TTDocument.pm _test/Resources/TTView/My/Org/Panel.tt _test/Test/Web/View.pm
diffstat 4 files changed, 42 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/Lib/IMPL/Profiler/Memory.pm	Wed Apr 04 02:49:45 2012 +0400
+++ b/Lib/IMPL/Profiler/Memory.pm	Wed Apr 04 17:51:27 2012 +0400
@@ -57,17 +57,19 @@
 use Data::Dumper();
 use Scalar::Util qw(refaddr weaken isweak);
 
-use fields qw( objects );
+use fields qw( objects counter);
 
 sub CTOR {
 	my $this = shift;
 	$this->{objects} = [];
+	$this->{counter} = 0;
 }
 
 sub track {
 	my $i = scalar @{$_[0]->{objects}};
 	$_[0]->{objects}[$i] = $_[1];
 	weaken($_[0]->{objects}[$i]);
+	$_[0]->{counter} ++;
 }
 
 sub Purge {
--- a/Lib/IMPL/Web/View/TTDocument.pm	Wed Apr 04 02:49:45 2012 +0400
+++ b/Lib/IMPL/Web/View/TTDocument.pm	Wed Apr 04 17:51:27 2012 +0400
@@ -6,6 +6,8 @@
 use IMPL::Web::View::TTFactory();
 use IMPL::Web::View::TTControl();
 
+use Scalar::Util qw(weaken);
+
 
 use parent qw(
 	IMPL::DOM::Document
@@ -33,6 +35,17 @@
 	
 	$this->opts($refOpts);
 	$this->stash($this->context->stash);
+	
+	my $self = $this;
+	weaken($self);
+	
+	$this->templateVars('require', sub {
+		my $doc = $self;
+		die new IMPL::Exception("A document is destroyed or invalid") unless $doc;
+		$doc->require(@_);
+	});
+	
+	$this->templateVars('document', sub { $self } );
 }
 
 our %CTOR = (
@@ -98,15 +111,27 @@
 sub Render {
 	my ($this,$args) = @_;
 	
-	my $output = $this->SUPER::Render( { document => $this } );
+	my $output;
 	
 	if ($this->layout) {
-		$output = $this->context->include($this->layout, { content => $output } );
+		$output = $this->context->include(
+			$this->layout,
+			{
+				content => sub { $output ||= $this->RenderContent($args); }
+			}
+		);
+	} else {
+		return $this->RenderContent($args);
 	}
 	
 	return $output;
 }
 
+sub RenderContent {
+	my $this = shift;
+	return $this->SUPER::Render(@_);
+}
+
 
 1;
 
@@ -153,8 +178,7 @@
 использован как шаблон для форматирования объектной модели, вывод самого шаблона будет проигнорирован. Если
 обертка не задана, то результатом будет вывод самого скрипта шаблона.
 
-Каждый документ имеет свое собственное пространство имен, которое может быть вложенным в некоторое внешнее,
-указанное при создании документа.
+
 =head2 Порядок обработки документа
 
 =over
@@ -172,7 +196,7 @@
 
 =over
 
-=item 1 C<document.require('my/org/input')>
+=item 1 C<require('my/org/input')>
 
 =item 1 Загружает шаблон C<my/org/input.tt>
 
--- a/_test/Resources/TTView/My/Org/Panel.tt	Wed Apr 04 02:49:45 2012 +0400
+++ b/_test/Resources/TTView/My/Org/Panel.tt	Wed Apr 04 17:51:27 2012 +0400
@@ -3,19 +3,17 @@
 	BLOCK INIT;
 		dojoDefaultClass = 'dijit.form.Input'; 
 		dojo.require.push( dojoDefaultClass );
+		TPreview = require('My/Org/TextPreview');
 	END;
 	BLOCK CTOR;
 		dojoClass = dojoDefaultClass;
-		visualClass = this.nodeProperty('visualClass') || 'classic';
+		visualClass = this.visualClass || 'classic';
+		FOREACH text IN data;
+			CALL this.appendChild(TPreview.new('preview', nodeValue = text ));
+		END;
 	END;
 %]
 [% BLOCK RENDER %]
-[%
-	TPreview = document.require('My/Org/TextPreview');
-	FOREACH text IN data;
-		CALL this.appendChild(TPreview.new('preview', nodeValue = text ));
-	END;
-%]
 <div class="$visualClass" data-dojo-type="$dojoClass">
 	[% FOREACH node IN this.selectNodes('preview') %]
 		[% node.Render() %]
--- a/_test/Test/Web/View.pm	Wed Apr 04 02:49:45 2012 +0400
+++ b/_test/Test/Web/View.pm	Wed Apr 04 17:51:27 2012 +0400
@@ -22,7 +22,7 @@
 	my $code = shift;
 	my $dump = shift;
 	
-	my $data = MProfiler->Monitor($code, sub { $_ =~ m/^IMPL::/} );
+	my $data = MProfiler->Monitor($code);
 	
 	if ($data->isLeak and $dump) {
 		write_file("dump.out", { binmode => ':utf8' }, $data->Dump() );
@@ -108,6 +108,7 @@
 	my $loader = $this->CreateLoader();
 	
 	my $doc = $loader->document('simple');
+	$doc->templateVars(data => ['one','two','hello world']);
 	
 	assert(defined $doc);
 	
@@ -132,7 +133,9 @@
 	
 	$doc->appendChild($ctl);
 	
-	my $text = $ctl->Render({ data => ['one','two','hello world']});
+	
+	
+	my $text = $ctl->Render();
 	my $expected = read_file($this->GetResourceFile('Resources', 'TTView.Output', 'Panel.txt'), binmode => ':utf8');
 	
 	assert($text eq $expected, '$ctl->Render(): Bad output', "Got:      $text", "Expected: $expected");