comparison Lib/IMPL/Web/View/TTControl.pm @ 191:78a18a2b6266

IMPL::Web::View improvements (unstable)
author cin
date Thu, 05 Apr 2012 17:51:51 +0400
parents 08015e2803f1
children a9faf883cdce
comparison
equal deleted inserted replaced
190:cd1ff7029a63 191:78a18a2b6266
3 3
4 use IMPL::DOM::Property qw(_dom); 4 use IMPL::DOM::Property qw(_dom);
5 use IMPL::lang qw(:declare :constants); 5 use IMPL::lang qw(:declare :constants);
6 6
7 use Template::Context(); 7 use Template::Context();
8 use Scalar::Util qw(weaken);
8 9
9 use parent qw( 10 use parent qw(
10 IMPL::DOM::Node 11 IMPL::DOM::Node
11 ); 12 );
12 13
32 $name ||= "control"; 33 $name ||= "control";
33 34
34 $this->template( $template ) or die new IMPL::ArgumentException("A template is required"); 35 $this->template( $template ) or die new IMPL::ArgumentException("A template is required");
35 $this->context( $context ) or die new IMPL::ArgumentException("A context is required"); 36 $this->context( $context ) or die new IMPL::ArgumentException("A context is required");
36 37
37 if ( my $ctor = $template->blocks->{CTOR} ) { 38 $this->id($name . "-" . _GetNextId()) unless $this->id;
38 $context->process($ctor, { this => $this } );
39 $context->stash->set('this',undef);
40 }
41 39
42 $this->id($name . "-" . _GetNextId()) unless $this->id; 40 weaken($this); # prevent cyclic references produces by the code below
41
42 $context->stash->set('append', sub { $this->appendChild(@_); undef; } );
43 $context->stash->set('select', sub { $this->selectNodes(@_) } );
43 44
44 } 45 }
45 46
46 our %CTOR = ( 47 our %CTOR = (
47 'IMPL::DOM::Node' => sub { 48 'IMPL::DOM::Node' => sub {
48 nodeName => $_[0], 49 nodeName => $_[0],
49 %{ $_[3] || {} } 50 %{ $_[3] || {} }
50 } 51 }
51 ); 52 );
52 53
54 sub InitInstance {
55 my ($this,$args) = @_;
56
57 $args ||= {};
58
59 if ( my $ctor = $this->template->blocks->{CTOR} ) {
60 $this->context->process($ctor, { %$args, this => $this } );
61 $this->context->stash->set('this',undef);
62 }
63 }
64
53 sub renderBlock { 65 sub renderBlock {
54 $_[0]->template->blocks->{RENDER}; 66 $_[0]->template->blocks->{RENDER} || $_[0]->template;
55 } 67 }
56 68
57 sub Render { 69 sub Render {
58 my ($this,$args) = @_; 70 my ($this,$args) = @_;
59 71