Commit | Line | Data |
931cbc8d |
1 | package Reaction::UI::Controller::Role::Action::Create; |
2 | |
3 | use Moose::Role -traits => 'MethodAttributes'; |
4 | use Reaction::UI::ViewPort::Action; |
5 | |
6 | requires qw/get_collection make_context_closure setup_viewport/; |
7 | |
8 | sub create :Action :Args(0) { |
9 | my ($self, $c) = @_; |
10 | my $target = $c->stash->{collection} || $self->get_collection($c); |
11 | my %vp_args = ( model => $target->action_for('Create') ); |
12 | |
13 | if( $self->can('on_create_apply_callback') ){ |
14 | my $apply = sub { $self->on_create_apply_callback( @_) }; |
15 | $vp_args{on_apply_callback} = $self->make_context_closure( $apply ); |
16 | } |
17 | if( $self->can('on_create_close_callback') ){ |
18 | my $close = sub { $self->on_create_close_callback( @_) }; |
19 | $vp_args{on_close_callback} = $self->make_context_closure( $close ); |
20 | } |
21 | |
22 | $self->setup_viewport( $c, \%vp_args ); |
23 | } |
24 | |
25 | around _build_action_viewport_map => sub { |
26 | my $orig = shift; |
27 | my $map = shift->$orig( @_ ); |
28 | $map->{create} = 'Reaction::UI::ViewPort::Action'; |
29 | return $map; |
30 | }; |
31 | |
32 | 1; |
33 | |
34 | __END__; |
35 | |
36 | =head1 NAME |
37 | |
38 | Reaction::UI::Controller::Role::Action::Create - Create action |
39 | |
40 | =head1 DESCRIPTION |
41 | |
42 | Provides a C<create> action, which sets up an L<Action Viewport|Reaction::UI::Viewport::Action> |
43 | by calling C<action_for> on either the object located in the C<collection> slot |
44 | of the C<stash> or on the object returned by the method C<get_collection>. |
45 | |
46 | =head1 SYNOPSYS |
47 | |
48 | package MyApp::Controller::Foo; |
49 | |
50 | use base 'Reaction::Controller'; |
51 | use Reaction::Class; |
52 | |
53 | with( |
54 | 'Reaction::UI::Controller::Role::GetCollection', |
55 | 'Reaction::UI::Controller::Role::Action::Simple', |
56 | 'Reaction::UI::Controller::Role::Action::Create' |
57 | ); |
58 | |
59 | __PACKAGE__->config( action => { |
60 | create => { Chained => 'base' }, |
61 | } ); |
62 | |
63 | sub base :Chained('/base') :CaptureArgs(0) { |
64 | ... |
65 | } |
66 | |
67 | sub on_create_apply_callback{ #optional callback |
68 | my($self, $c, $vp, $result) = @_; |
69 | ... |
70 | } |
71 | |
72 | sub on_create_close_callback{ #optional callback |
73 | my($self, $c, $vp) = @_; |
74 | ... |
75 | } |
76 | |
77 | =head1 ROLES CONSUMED |
78 | |
79 | This role also consumes the following roles: |
80 | |
81 | =over4 |
82 | |
83 | =item L<Reaction::UI::Controller::Role::Action::Simple> |
84 | |
85 | =back |
86 | |
87 | =head1 REQUIRED METHODS |
88 | |
89 | The following methods must be provided by the consuming class: |
90 | |
91 | =over4 |
92 | |
93 | =item C<get_collection> |
94 | |
95 | =item C<make_context_closure> |
96 | |
97 | =back |
98 | |
99 | =head1 ACTIONS |
100 | |
101 | =head2 create |
102 | |
103 | Chain endpoint with no args, sets up the viewport with the appropriate action. |
104 | If the methods C<on_create_apply_callback> and C<on_create_close_callback> are |
105 | present in the consuming class, they will be used as callbacks in the viewport. |
106 | |
107 | =head1 METHODS |
108 | |
109 | =head2 _build_action_viewport_map |
110 | |
111 | Extends to set the C<create> key in the map to L<Reaction::UI::ViewPort::Action> |
112 | |
113 | =head1 SEE ALSO |
114 | |
115 | =over4 |
116 | |
117 | =item L<Reaction::UI::Controller> |
118 | |
119 | =item L<Reaction::UI::Controller::Role::GetCollection> |
120 | |
121 | =item L<Reaction::UI::Controller::Role::Action::Simple> |
122 | |
123 | =item L<Reaction::UI::Controller::Role::Action::List> |
124 | |
125 | =item L<Reaction::UI::Controller::Role::Action::View> |
126 | |
127 | =item L<Reaction::UI::Controller::Role::Action::Object> |
128 | |
129 | =item L<Reaction::UI::Controller::Role::Action::Update> |
130 | |
131 | =item L<Reaction::UI::Controller::Role::Action::Delete> |
132 | |
133 | =item L<Reaction::UI::Controller::Role::Action::DeleteAll> |
134 | |
135 | =back |
136 | |
137 | =head1 AUTHORS |
138 | |
139 | See L<Reaction::Class> for authors. |
140 | |
141 | =head1 LICENSE |
142 | |
143 | See L<Reaction::Class> for the license. |
144 | |
145 | =cut |