NAME
Mojolicious::Plugin::Notifications::JSON - Event Notifications in JSON
SYNOPSIS
# Register the engine
plugin Notifications => {
JSON => 1
};
# In the controller
$c->render(json => $c->notifications(json => $json));
DESCRIPTION
This plugin is a simple notification engine for JSON.
If it does not suit your needs, you can easily write your own engine.
METHODS
Mojolicious::Plugin::Notifications::JSON inherits all methods from Mojolicious::Plugin::Notifications::Engine and implements the following new one.
register
plugin Notifications => {
JSON => {
key => 'notes'
}
};
Called when registering the main plugin. All parameters under the key JSON
are passed to the registration.
Accepts the following parameters:
- key
-
Define the attribute name of the notification array. Defaults to
notifications
.
HELPERS
notify
$c->notify(warn => 'wrong');
$c->notify(confirm => { ok => '/ok', cancel => '/cancel' } => 'Please, confirm!');
$c->render(json => $c->notifications(json => { text => 'html' }));
# {
# "notifications":[
# ["warn", "wrong"],
# [
# "confirm",
# "Please, confirm!",
# {
# "cancel":{
# "method":"POST",
# "url":"\/cancel"
# },
# "ok":{
# "method":"POST",
# "url":"\/ok"
# }
# }
# ]
# ],
# "text":"example"
# }
See the base notify helper.
In case an ok
, cancel
or confirm
parameter is passed, this will add an object containing confirmation and/or cancellation paths. The confirm
URL should point to an (HTML) endpoint a user can enter confirmation details. If it is given, both the ok
and cancel
parameters will be ignored. In case an ok_label
is passed, this will be the key for the confirmation object. In case a cancel_label
is passed, this will be the key for the cancelation object. In case a confirm_label
is passed, this will be the key for the confirmation object.
Confirmation routes for JSON do not support CSRF protection.
Confirmation is EXPERIMENTAL!
notifications
$c->render(json => $c->notifications(json => $json));
$c->render(json => $c->notifications(json => $json, key => 'notes'));
Merge notifications into your JSON response.
In case JSON is an object, it will inject an attribute that points to an array reference containing the notifications. If the JSON is an array, an object is appended with one attribute that points to an array reference containing the notifications. If the JSON is empty, an object will be created with one attribute that points to an array reference containing the notifications.
If the JSON is not of one of the descripted types, it's returned unaltered.
The name of the attribute can either be given on registration or by passing a parameter for key
. The name defaults to notifications
.
AVAILABILITY
https://github.com/Akron/Mojolicious-Plugin-Notifications
COPYRIGHT AND LICENSE
Copyright (C) 2014-2018, Nils Diewald.
This program is free software, you can redistribute it and/or modify it under the terms of the Artistic License version 2.0.