Revision b9740e8a
Von Moritz Bunkus vor mehr als 11 Jahren hinzugefügt
SL/ClientJS.pm | ||
---|---|---|
9 | 9 |
|
10 | 10 |
use Rose::Object::MakeMethods::Generic |
11 | 11 |
( |
12 |
'scalar --get_set_init' => [ qw(_actions) ], |
|
12 |
'scalar --get_set_init' => [ qw(_actions _flash _error) ],
|
|
13 | 13 |
); |
14 | 14 |
|
15 | 15 |
my %supported_methods = ( |
16 |
# ## Non-jQuery methods ## |
|
17 |
flash => 2, # display_flash(<TARGET>, <ARGS>) |
|
18 |
|
|
16 | 19 |
# ## jQuery basics ## |
17 | 20 |
|
18 | 21 |
# Basic effects |
... | ... | |
123 | 126 |
return []; |
124 | 127 |
} |
125 | 128 |
|
129 |
sub init__flash { |
|
130 |
return {}; |
|
131 |
} |
|
132 |
|
|
133 |
sub init__error { |
|
134 |
return ''; |
|
135 |
} |
|
136 |
|
|
126 | 137 |
sub to_json { |
127 | 138 |
my ($self) = @_; |
139 |
|
|
140 |
return SL::JSON::to_json({ error => $self->_error }) if $self->_error; |
|
128 | 141 |
return SL::JSON::to_json({ eval_actions => $self->_actions }); |
129 | 142 |
} |
130 | 143 |
|
... | ... | |
144 | 157 |
return $self; |
145 | 158 |
} |
146 | 159 |
|
160 |
sub flash { |
|
161 |
my ($self, $type, @messages) = @_; |
|
162 |
|
|
163 |
my $message = join ' ', grep { $_ } @messages; |
|
164 |
|
|
165 |
if (!$self->_flash->{$type}) { |
|
166 |
$self->_flash->{$type} = [ 'flash', $type, $message ]; |
|
167 |
push @{ $self->_actions }, $self->_flash->{$type}; |
|
168 |
} else { |
|
169 |
$self->_flash->{$type}->[-1] .= ' ' . $message; |
|
170 |
} |
|
171 |
|
|
172 |
return $self; |
|
173 |
} |
|
174 |
|
|
175 |
sub error { |
|
176 |
my ($self, @messages) = @_; |
|
177 |
|
|
178 |
$self->_error(join ' ', grep { $_ } ($self->_error, @messages)); |
|
179 |
|
|
180 |
return $self; |
|
181 |
} |
|
182 |
|
|
147 | 183 |
1; |
148 | 184 |
__END__ |
149 | 185 |
|
... | ... | |
301 | 337 |
|
302 | 338 |
The first variation is obviously better suited for chaining. |
303 | 339 |
|
340 |
Additional functions: |
|
341 |
|
|
342 |
=over 4 |
|
343 |
|
|
344 |
=item C<flash $type, $message> |
|
345 |
|
|
346 |
Display a C<$message> in the flash of type C<$type>. Multiple calls of |
|
347 |
C<flash> on the same C<$self> will be merged by type. |
|
348 |
|
|
349 |
On the client side the flash of this type will be cleared before the |
|
350 |
message is shown. |
|
351 |
|
|
352 |
=item C<error $message> |
|
353 |
|
|
354 |
Causes L<to_json> (and therefore L<render>) to output a JSON object |
|
355 |
that only contains an C<error> field set to this C<$message>. The |
|
356 |
client will then show the message in the 'error' flash. |
|
357 |
|
|
358 |
The messages of multiple calls of C<error> on the same C<$self> will |
|
359 |
be merged. |
|
360 |
|
|
361 |
=back |
|
362 |
|
|
304 | 363 |
=head2 JQUERY FUNCTIONS |
305 | 364 |
|
306 | 365 |
The following jQuery functions are supported: |
Auch abrufbar als: Unified diff
Error-Handling: Bei AJAX-Requests Fehler als JSON-Object zurückgeben