Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision b9740e8a

Von Moritz Bunkus vor mehr als 11 Jahren hinzugefügt

  • ID b9740e8a9a77eafcaf7aacd530af23fa8dbcb9f9
  • Vorgänger de66b95f
  • Nachfolger 001c83b1

Error-Handling: Bei AJAX-Requests Fehler als JSON-Object zurückgeben

Unterschiede anzeigen:

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:
SL/Dispatcher.pm
30 30
use SL::LXDebug;
31 31
use SL::LxOfficeConf;
32 32
use SL::Locale;
33
use SL::ClientJS;
33 34
use SL::Common;
34 35
use SL::Form;
35 36
use SL::Helper::DateTime;
......
74 75
  }
75 76
}
76 77

  
78
sub render_error_ajax {
79
  my ($error) = @_;
80

  
81
  SL::ClientJS->new
82
    ->error($error)
83
    ->render(SL::Controller::Base->new);
84
}
85

  
77 86
sub show_error {
78 87
  $::lxdebug->enter_sub;
79 88
  my $template             = shift;
......
85 94
  $::form->{error}         = $::locale->text('The session is invalid or has expired.') if ($error_type eq 'session');
86 95
  $::form->{error}         = $::locale->text('Incorrect password!')                    if ($error_type eq 'password');
87 96

  
97
  return render_error_ajax($::form->{error}) if $::request->is_ajax;
98

  
88 99
  $::form->header;
89 100
  print $::form->parse_html_template($template, \%params);
90 101
  $::lxdebug->leave_sub;
......
272 283
    1;
273 284
  } or do {
274 285
    if ($EVAL_ERROR ne END_OF_REQUEST) {
275
      print STDERR $EVAL_ERROR;
276
      $::form->{label_error} = $::request->{cgi}->pre($EVAL_ERROR);
277
      chdir SL::System::Process::exe_dir;
278
      eval { show_error('generic/error') };
286
      my $error = $EVAL_ERROR;
287
      print STDERR $error;
288

  
289
      if ($::request->is_ajax) {
290
        eval { render_error_ajax($error) };
291
      } else {
292
        $::form->{label_error} = $::request->{cgi}->pre($error);
293
        chdir SL::System::Process::exe_dir;
294
        eval { show_error('generic/error') };
295
      }
279 296
    }
280 297
  };
281 298

  
SL/Form.pm
704 704
    return;
705 705
  }
706 706

  
707
  if ($::request->is_ajax) {
708
    $::lxdebug->message(0, "trying to render AJAX response...");
709
    SL::ClientJS->new
710
      ->error($error)
711
      ->render(SL::Controller::Base->new);
712
    ::end_of_request();
713
  }
714

  
707 715
  my $add_params = {
708 716
    'title_error' => $params{title},
709 717
    'label_error' => $error,
js/client_js.js
4 4
// "scripts/generate_client_js_actions.pl". See the documentation for
5 5
// SL/ClientJS.pm for instructions.
6 6

  
7
function display_flash(type, message) {
8
  $('#flash_' + type + '_content').text(message);
9
  $('#flash_' + type).show();
10
}
11

  
7 12
function eval_json_result(data) {
8 13
  if (!data)
9 14
    return;
10 15

  
16
  if (data.error)
17
    return display_flash('error', data.error);
18

  
19
  $('#flash_error').hide();
20
  $('#flash_error_content').empty();
21

  
11 22
  if ((data.js || '') != '')
12 23
    eval(data.js);
13 24

  
......
15 26
    $(data.eval_actions).each(function(idx, action) {
16 27
      // console.log("ACTION " + action[0] + " ON " + action[1]);
17 28

  
29
      // ## Non-jQuery methods ##
30
           if (action[0] == 'flash')                display_flash(action[1], action[2]);
31

  
18 32
      // ## jQuery basics ##
33

  
19 34
      // Basic effects
20
           if (action[0] == 'hide')                 $(action[1]).hide();
35
      else if (action[0] == 'hide')                 $(action[1]).hide();
21 36
      else if (action[0] == 'show')                 $(action[1]).show();
22 37
      else if (action[0] == 'toggle')               $(action[1]).toggle();
23 38

  
......
94 109

  
95 110
  // console.log("current_content_type " + $('#current_content_type').val() + ' ID ' + $('#current_content_id').val());
96 111
}
112

  
113
// Local Variables:
114
// mode: js
115
// End:
scripts/generate_client_js_actions.tpl
4 4
// "scripts/generate_client_js_actions.pl". See the documentation for
5 5
// SL/ClientJS.pm for instructions.
6 6

  
7
function display_flash(type, message) {
8
  $('#flash_' + type + '_content').text(message);
9
  $('#flash_' + type).show();
10
}
11

  
7 12
function eval_json_result(data) {
8 13
  if (!data)
9 14
    return;
10 15

  
16
  if (data.error)
17
    return display_flash('error', data.error);
18

  
19
  $('#flash_error').hide();
20
  $('#flash_error_content').empty();
21

  
11 22
  if ((data.js || '') != '')
12 23
    eval(data.js);
13 24

  
......
20 31

  
21 32
  // console.log("current_content_type " + $('#current_content_type').val() + ' ID ' + $('#current_content_id').val());
22 33
}
34

  
35
// Local Variables:
36
// mode: js
37
// End:

Auch abrufbar als: Unified diff