connect-badass-messages

Flash message Connect middleware + Express view helpers.

npm install connect-badass-messages
3 downloads in the last month

connect-badass-messages

Installing

npm install connect-badass-messages

Why not use connect-flash, etc.?

Not trying to get in a module battle here or anything. Just getting mad informational on ya.

1) Message persistence.

connect-flash clears the list of messages associated with the current request the first time you call req.flash(). This is probably okay most of the time, but then again, maybe not.

connect-badass-messages doesn't clear the list of messages to be displayed on the current page render until the request is complete. Which means that if you need to call getMessages() more than once in your application for whatever reason, you're in luck. That call will return the exact same list of messages every time during a given request.

2) Asynchronous/synchronous versions of getMessages().

connect-flash only offers a synchronous version of its message-getter function. If you happen to need to grab your messages in conjunction with some other asynchronous functionality, it's kind of nice to be able to write

require('async').parallel({
  firstTask: function(cb) { ... },
  secondTask: function(cb) { ... },
  getAllMessages: req.getMessages,
  ...
});

3) Includes the same Express dynamicHelpers functionality as express-messages, and more!

By switching from connect-flash, you're not losing any Express ease-of-use. Plus, you can choose whether to have messages returned to your templates

  1. in an array...
  2. formatted for Twitter's Bootstrap CSS/JS framework, or...
  3. with custom formatting!
  4. (or all of the above -- look at the example below)
app.dynamicHelpers({ 
  messagesAsArray: badassMessages.expressDynamicHelper(),
  messagesFormattedForBootstrap: badassMessages.expressDynamicHelper(true),
  messagesWithCustomFormatting: badassMessages.expressDynamicHelper(function(item) {
    return "<div class='message-" + item.type + "'>" + item.message + "</div>";
  })
});

How to use

Make sure you have the connect-session middleware configured.

var badassMessages = require('connect-badass-messages');

app.use(connect.session({ secret: 'xyzzy' });
app.use(badassMessages.middleware());

Now the req object has a getMessages([type], [formatted], [cb]) function and an addMessage(message [, type]) function. If the formatted argument is specified, it must either be true/false (true will use the default Twitter Bootstrap formatter on your messages) or you can specify a custom formatter function (see the section on custom formatters below).

req.addMessage(message [, type])

Pretty self explanatory. The default type is 'info'.

req.getMessages() synchronously

// Filter by type, return as list.
var errorMessages = req.getMessages('error');

// Filter by type, return with default formatting.
var errorMessages = req.getMessages('error', true);

// Filter by type, return with custom formatting
// (note the 'null' param at the end!)
var errorMessages = req.getMessages('error', function(message, cb) {
  // ...
}, null);

// All messages
var errorMessages = req.getMessages();

// All messages, default formatting
var errorMessages = req.getMessages(true);

// All messages, custom formatting
// (note the 'null' param at the end!)
var errorMessages = req.getMessages(function(message, cb) {
  // ...
}, null);

req.getMessages() asynchronously

// Filter by type, return as list.
req.getMessages('error', function(err, messages) {
  // ...
});

// Filter by type, return with default formatting.
req.getMessages('error', true, function(err, messages) {
  // ...
});

// Filter by type, return with custom formatting
req.getMessages('error',
                function(message, cb) {
                  // ...
                },
                function(err, messages) {
                  // ...
                });

// All messages
req.getMessages(function(err, messages) {
  // ...
});

// All messages, default formatting
req.getMessages(true, function(err, messages) {
  // ...
});

// All messages, custom formatting
req.getMessages(function(message, cb) {
                  // ...
                },
                function(err, messages) {
                  // ...
                });

Custom formatter functions

Write your custom formatter functions in a flexible way so that when you call getMessages(), you'll have the option of using both the synchronous and asynchronous versions of that function.

For example:

function customFormatter(message, cb) {
  var formatted = message.type + ": " + message.message;
  if (cb) { cb(null, formatted); }
  else { return formatted; }
}

Also, take note that the custom formatter function's signature must match the function signature of the async module's map function. Two guesses as to why.

Custom formatter functions and expressDynamicHelper()

It's particularly important to make sure that your custom formatter function can handle the synchronous case because that's how Express's dynamicHelpers work. If you only handle the async case, you'll see blank output in your templates instead of messages.

License

(The MIT License)

Copyright (c) 2012 bryn austin bellomy bryn@signals.io

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

npm loves you