1 package Reaction::UI::Controller;
3 use base qw(Catalyst::Controller); # Reaction::Object);
6 use Scalar::Util 'weaken';
7 use namespace::clean -except => [ qw(meta) ];
9 has context => (is => 'ro', isa => 'Object', weak_ref => 1);
10 with 'Catalyst::Component::InstancePerContext';
12 sub build_per_context_instance {
13 my ($self, $c, @args) = @_;
14 my $newself = $self->new($self->_application, {%$self, context => $c, @args});
20 my $c = $self->context;
21 my $focus_stack = $c->stash->{focus_stack};
22 my ($class, @proto_args) = @_;
24 if (my $vp_attr = $c->stack->[-1]->attributes->{ViewPort}) {
25 if (ref($vp_attr) eq 'ARRAY') {
26 $vp_attr = $vp_attr->[0];
28 if (ref($vp_attr) eq 'HASH') {
29 if (my $conf_class = delete $vp_attr->{class}) {
32 %args = %{ $self->merge_config_hashes($vp_attr, {@proto_args}) };
43 if (exists $args{next_action} && !ref($args{next_action})) {
44 $args{next_action} = [ $self, 'redirect_to', $args{next_action} ];
46 $focus_stack->push_viewport($class, %args);
50 return shift->context->stash->{focus_stack}->pop_viewport;
53 sub pop_viewports_to {
55 return $self->context->stash->{focus_stack}->pop_viewports_to($vp);
59 my ($self, $c, $to, $cap, $args, $attrs) = @_;
61 #the confess calls could be changed later to $c->log ?
63 my $reftype = ref($to);
65 $action = $self->action_for($to);
66 confess("Failed to locate action ${to} in " . blessed($self)) unless $action;
67 } elsif($reftype eq 'ARRAY' && @$to == 2){ #is that overkill / too strict?
68 $action = $c->controller($to->[0])->action_for($to->[1]);
69 confess("Failed to locate action $to->[1] in $to->[0]" ) unless $action;
70 } elsif( blessed $to && $to->isa('Catalyst::Action') ){
73 confess("Failed to locate action from ${to}");
76 $cap ||= $c->req->captures;
77 $args ||= $c->req->args;
79 my $uri = $c->uri_for($action, $cap, @$args, $attrs);
80 $c->res->redirect($uri);
83 sub make_context_closure {
84 my($self, $closure) = @_;
85 my $ctx = $self->context;
87 return sub { $closure->($ctx, @_) };
96 Reaction::UI::Controller - Reaction Base Controller Class
100 package MyApp::Controller::Foo;
103 use parent 'Reaction::UI::Controller';
105 use aliased 'Reaction::UI::ViewPort';
107 sub foo: Chained('/base') Args(0) {
108 my ($self, $ctx) = @_;
110 $ctx->push_viewport(ViewPort,
119 Base Reaction Controller class. Inherits from:
123 =item L<Catalyst::Controller>
124 =item L<Catalyst::Component::ACCEPT_CONTEXT>
125 =item L<Reaction::Object>
131 =head2 push_viewport $vp_class, %args
133 Creates a new instance of the L<Reaction::UI::ViewPort> class
134 ($vp_class) using the rest of the arguments given (%args). Defaults of
135 the action can be overridden by using the C<ViewPort> key in the
136 controller configuration. For example to override the default number
137 of items in a CRUD list action:
141 list => { ViewPort => { per_page => 50 } },
145 The ViewPort is added to the L<Reaction::UI::Window>'s FocusStack in
146 the stash, and also returned to the calling code.
152 =item L<Reaction::UI::Controller::Root>
153 =item L<Reaction::UI::Window>
157 TODO: explain how next_action as a scalar gets converted to the redirect arrayref thing
161 =head2 pop_viewport_to $vp
163 Call L<Reaction::UI::FocusStack/pop_viewport> or
164 L<Reaction::UI::FocusStack/pop_viewport_to> on
165 the C<< $c->stash->{focus_stack} >>.
167 =head2 redirect_to $c, $to, $captures, $args, $attrs
169 Construct a URI and redirect to it.
175 =item The name of an action in the current controller.
177 =item A L<Catalyst::Action> instance.
179 =item An arrayref of controller name and the name of an action in that
184 $captures and $args default to the current requests $captures and
185 $args if not supplied.
187 =head2 make_context_closure
189 The purpose of this method is to prevent memory leaks.
190 It weakens the context object, often denoted $c, and passes it as the
191 first argument to the sub{} that is passed to the make_context_closure method.
196 make_context_closure returns sub { $sub_you_gave_it->($weak_c, @_)
200 To further expound up this useful construct consider code written before
201 make_context_closure was created:
205 $self->after_search( $c, @_ );
209 This could be rewritten as:
211 on_apply_callback => $self->make_context_closure(
214 $self->after_search( $weak_c, @_ );
218 Or even more succintly:
220 on_apply_callback => $self->make_context_closure(
222 $self->after_search( @_ );
228 See L<Reaction::Class> for authors.
232 See L<Reaction::Class> for the license.