NAME

Dancer2::Plugin::Growler - Growl multiple messages of varying types to the user on their next hit.

VERSION

This document describes Dancer2::Plugin::Growler version 0.03

SYNOPSIS

use Dancer2::Plugin::Growler;

…

my $error = locale->maketext('Invalid login credentials.'); # locale() is from L<Dancer2::Plugin::Locale>
growl_error($error); 
redirect '/login';

…

my $msg = locale->maketext('Successfully created post the post “[_1]”.', $html_safe_title); # locale() is from L<Dancer2::Plugin::Locale>
growl_success($msg); 
redirect "/post/$new_id";

Then in the view’s layout (this example implies a bootstrap3/jquery environment w/ the bootstrapGrowl jquery plugin):

[% SET growl_list = growls() %]
[%- IF growl_list.size %]
[% USE JSON.Escape %]
<script type="text/javascript">
    $( document ).ready(function() {
        [% FOREACH growl in growl_list -%]
            $.bootstrapGrowl("[% growl.message.dquote %]", [% growl.options.json %]);
        [% END %]
    });
</script>
[%- END %]

DESCRIPTION

This allows you to specify one or more one-time messages (of varying types) to growl on the user’s next hit (or this hit if you render a view that implements it) with out needing to pass around parameters.

It is also a nice approach because it is refresh safe. For example, in the SYNOPSIS example with the blog post, say we rendered the message at the end of the request instead: refreshing the page could cause us to post the blog article again.

It is similar to the “flash” example in the Dancer2 documentation but without the multi-user race condition and limitation of one message.

It is also AJAX-safe (don’t call growls() in your AJAX templates and you won’t miss any message) yet still AJAX-able (call growls() in your AJAX templates in order to include them in your response).

Also, with this approach your perl can growl exactly like you do in JavaScript with zero effort. Ease of Consistency FTW!

INTERFACE

growl()

The first argument is the message to growl. The second, optional argument is hashref of arguments.

The keys it can specify are:

'type'

The type of message. The values, if given, can be: 'info', 'success', 'warning', or 'danger' (That last one is from bootstrap convention).

If your javascript implementation uses a different key (or different values) for this then your system will need to factor that in.

'delay'

The number of milliseconds you intend the growl to display before fading out.

Zero should cause your JavaScript implementation to not fade, effectively making it permanent.

If your JavaScript implementation uses a different key for this then your system will need to factor that in.

'allow_dismiss'

Boolean of if the growl should be dismissible via a close icon.

If the delay is turned off this is forced to true so that we don’t end up with a permanent growl on the screen.

If your JavaScript implementation uses a different key for this then your system will need to factor that in.

(anything else your underlying javascript library can use)

I recommend leaving this to the templates and JavaScript to make maintenance easier and keep things consistent.

growl_info()

Same as growl() but forces a type of 'info'.

growl_success()

Same as growl() but forces a type of 'success'.

growl_warning()

Same as growl() but forces a type of 'warning'.

growl_error()

Same as growl() but forces a type of 'danger' (That is from bootstrap convention).

growls()

For the consumer to use in its templates (though it is availaable in perl also).

It returns the list of growls (if any) and clears the list.

Each growl consists of two keys:

'message'

The message to display.

'options'

The hashref of options for the view that can include 'delay' and 'allow_dismiss' noted above.

See the SYNOPSIS for an example.

DIAGNOSTICS

Throws no warnings or errors of its own.

CONFIGURATION AND ENVIRONMENT

Dancer2::Plugin::Growler requires no configuration files or environment variables.

DEPENDENCIES

Dancer2::Plugin

INCOMPATIBILITIES

None reported.

BUGS AND LIMITATIONS

No bugs have been reported.

Please report any bugs or feature requests to bug-dancer2-plugin-growler@rt.cpan.org, or through the web interface at http://rt.cpan.org.

SEE ALSO

Dancer2::Plugin::Deferred does a similar thing but, for my growling needs, it turned out to be overly complex and not flexible enough.

AUTHOR

Daniel Muey <http://drmuey.com/cpan_contact.pl>

LICENCE AND COPYRIGHT

Copyright (c) 2015, Daniel Muey <http://drmuey.com/cpan_contact.pl>. All rights reserved.

This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See perlartistic.

DISCLAIMER OF WARRANTY

BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR CORRECTION.

IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.