aboutsummaryrefslogtreecommitdiffstats
path: root/lib/Reaction/UI/Controller.pm
blob: 4777ccd01b249c0a494122244c247232f8193729 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package Reaction::UI::Controller;

use base qw(Catalyst::Controller); # Reaction::Object);

use Reaction::Class;
use Scalar::Util 'weaken';
use namespace::clean -except => [ qw(meta) ];

has context => (is => 'ro', isa => 'Object', weak_ref => 1);
with(
  'Catalyst::Component::InstancePerContext',
  'Reaction::UI::Controller::Role::RedirectTo'
);

sub build_per_context_instance {
  my ($self, $c, @args) = @_;
  my $class = ref($self) || $self;
  my $newself =  $class->new($self->_application, {%$self, context => $c, @args});
  return $newself;
}

sub push_viewport {
  my $self = shift;
  my $c = $self->context;
  my $focus_stack = $c->stash->{focus_stack};
  my ($class, @proto_args) = @_;
  my %args;
  if (my $vp_attr = $c->stack->[-1]->attributes->{ViewPort}) {
    if (ref($vp_attr) eq 'ARRAY') {
      $vp_attr = $vp_attr->[0];
    }
    if (ref($vp_attr) eq 'HASH') {
      $class = $vp_attr->{class} if defined $vp_attr->{class};
      %args = %{ $self->merge_config_hashes($vp_attr, {@proto_args}) };
    } else {
      $class = $vp_attr;
      %args = @proto_args;
    }
  } else {
    %args = @proto_args;
  }

  $args{ctx} = $c;

  if (exists $args{next_action} && !ref($args{next_action})) {
    $args{next_action} = [ $self, 'redirect_to', $args{next_action} ];
  }
  $focus_stack->push_viewport($class, %args);
}

sub pop_viewport {
  return shift->context->stash->{focus_stack}->pop_viewport;
}

sub pop_viewports_to {
  my ($self, $vp) = @_;
  return $self->context->stash->{focus_stack}->pop_viewports_to($vp);
}

sub make_context_closure {
  my($self, $closure) = @_;
  my $ctx = $self->context;
  weaken($ctx);
  return sub { $closure->($ctx, @_) };
}

1;

__END__;

=head1 NAME

Reaction::UI::Controller - Reaction Base Controller Class

=head1 SYNOPSIS

  package MyApp::Controller::Foo;
  use strict;
  use warnings;
  use parent 'Reaction::UI::Controller';

  use aliased 'Reaction::UI::ViewPort';

  sub foo: Chained('/base') Args(0) {
    my ($self, $ctx) = @_;

    $ctx->push_viewport(ViewPort,
      layout => 'foo',
    );
  }

  1;

=head1 DESCRIPTION

Base Reaction Controller class, subclass of L<Catalyst::Controller>.

=head1 ROLES CONSUMED

=over 4

=item L<Catalyst::Component::InstancePerContext>

=item L<Reaction::UI::Controller::Role::RedirectTo>

Please not that this functionality is now deprecated.

=back

=head1 METHODS

=head2 push_viewport $vp_class, %args

Creates a new instance of the L<Reaction::UI::ViewPort> class
($vp_class) using the rest of the arguments given (%args). Defaults of
the action can be overridden by using the C<ViewPort> key in the
controller configuration. For example to override the default number
of items in a CRUD list action:

__PACKAGE__->config(
                    action => { 
                        list => { ViewPort => { per_page => 50 } },
    }
  );

The ViewPort is added to the L<Reaction::UI::Window>'s FocusStack in
the stash, and also returned to the calling code.

Related items:

=over

=item L<Reaction::UI::Controller::Root>
=item L<Reaction::UI::Window>

=back

TODO: explain how next_action as a scalar gets converted to the redirect arrayref thing

=head2 pop_viewport

=head2 pop_viewport_to $vp

Call L<Reaction::UI::FocusStack/pop_viewport> or
L<Reaction::UI::FocusStack/pop_viewport_to> on 
the C<< $c->stash->{focus_stack} >>.

=head2 redirect_to $c, $to, $captures, $args, $attrs

Construct a URI and redirect to it.

$to can be:

=over

=item The name of an action in the current controller.

=item A L<Catalyst::Action> instance.

=item An arrayref of controller name and the name of an action in that
controller.

=back

$captures and $args default to the current requests $captures and
$args if not supplied.

=head2 make_context_closure

The purpose of this method is to prevent memory leaks.
It weakens the context object, often denoted $c, and passes it as the 
first argument to the sub{} that is passed to the make_context_closure method.
In other words,

=over 4

make_context_closure returns sub { $sub_you_gave_it->($weak_c, @_)

=back

To further expound up this useful construct consider code written before
make_context_closure was created:

    on_apply_callback => 
        sub {
          $self->after_search( $c, @_ );
        }
    ),

This could be rewritten as:

    on_apply_callback => $self->make_context_closure(
        sub {
            my $weak_c = shift;
            $self->after_search( $weak_c, @_ );
        }
    ),

Or even more succintly:

    on_apply_callback => $self->make_context_closure(
        sub {
            $self->after_search( @_ );
        }
    ),

=head1 AUTHORS

See L<Reaction::Class> for authors.

=head1 LICENSE

See L<Reaction::Class> for the license.

=cut