Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added flash messages

  • Loading branch information...
commit df0b954b3c4e83a69ab4cfe2d87f2fb1d0ffa162 1 parent 929f564
Alex Young authored
View
13 app.js
@@ -8,6 +8,9 @@ var express = require('express@1.0.0'),
User,
Settings = { development: {}, test: {}, production: {} };
+app.helpers(require('./helpers.js').helpers);
+app.dynamicHelpers(require('./helpers.js').dynamicHelpers);
+
// Converts a database connection URI string to
// the format connect-mongodb expects
function mongoStoreConnectionArgs() {
@@ -118,6 +121,7 @@ app.get('/documents.:format?', loadUser, function(req, res) {
break;
default:
+ req.flash('info', 'Document created');
res.render('documents/index.jade', {
locals: { documents: documents, currentUser: req.currentUser }
});
@@ -150,6 +154,7 @@ app.post('/documents.:format?', loadUser, function(req, res) {
break;
default:
+ req.flash('info', 'Document created');
res.redirect('/documents');
}
});
@@ -187,6 +192,7 @@ app.put('/documents/:id.:format?', loadUser, function(req, res, next) {
break;
default:
+ req.flash('info', 'Document updated');
res.redirect('/documents');
}
});
@@ -205,6 +211,7 @@ app.del('/documents/:id.:format?', loadUser, function(req, res, next) {
break;
default:
+ req.flash('info', 'Document deleted');
res.redirect('/documents');
}
});
@@ -222,6 +229,7 @@ app.post('/users.:format?', function(req, res) {
var user = new User(req.body.user);
function userSaved() {
+ req.flash('info', 'Your account has been created');
switch (req.params.format) {
case 'json':
res.send(user.__doc);
@@ -234,7 +242,7 @@ app.post('/users.:format?', function(req, res) {
}
function userSaveFailed() {
- // TODO: Show error messages
+ req.flash('error', 'Account creation failed');
res.render('users/new.jade', {
locals: { user: user }
});
@@ -256,7 +264,7 @@ app.post('/sessions', function(req, res) {
req.session.user_id = user.id;
res.redirect('/documents');
} else {
- // TODO: Show error
+ req.flash('error', 'Incorrect credentials');
res.redirect('/sessions/new');
}
});
@@ -264,6 +272,7 @@ app.post('/sessions', function(req, res) {
app.del('/sessions', loadUser, function(req, res) {
if (req.session) {
+ req.flash('info', 'You are now logged out');
req.session.destroy(function() {});
}
res.redirect('/sessions/new');
View
54 helpers.js
@@ -0,0 +1,54 @@
+exports.helpers = {
+ appName: 'Nodepad',
+ version: '0.1',
+
+ nameAndVersion: function(name, version) {
+ return name + ' v' + version;
+ }
+};
+
+function FlashMessage(type, messages) {
+ this.type = type;
+ this.messages = typeof messages === 'string' ? [messages] : messages;
+}
+
+FlashMessage.prototype = {
+ get icon() {
+ switch (this.type) {
+ case 'info':
+ return 'ui-icon-info';
+ case 'error':
+ return 'ui-icon-alert';
+ }
+ },
+
+ get stateClass() {
+ switch (this.type) {
+ case 'info':
+ return 'ui-state-highlight';
+ case 'error':
+ return 'ui-state-error';
+ }
+ },
+
+ toHTML: function() {
+ return '<div class="ui-widget flash">' +
+ '<div class="' + this.stateClass + ' ui-corner-all">' +
+ '<p><span class="ui-icon ' + this.icon + '"></span>' + this.messages.join(', ') + '</p>' +
+ '</div>' +
+ '</div>';
+ }
+};
+
+exports.dynamicHelpers = {
+ flashMessages: function(req, res) {
+ var html = '';
+ ['error', 'info'].forEach(function(type) {
+ var messages = req.flash(type);
+ if (messages.length > 0) {
+ html += new FlashMessage(type, messages).toHTML();
+ }
+ });
+ return html;
+ }
+};
View
9 public/javascripts/application.js
@@ -93,6 +93,15 @@
});
});
+ function hideFlashMessages() {
+ $(this).fadeOut();
+ }
+
+ setTimeout(function() {
+ $('.flash').each(hideFlashMessages);
+ }, 5000);
+ $('.flash').click(hideFlashMessages);
+
$(window).resize(resize);
$(window).focus(resize);
resize();
View
23 public/stylesheets/style.css
@@ -171,3 +171,26 @@ ul.toolbar a {
.page p {
margin: 10px 0;
}
+.flash {
+ position: absolute;
+ top: 0;
+ bottom: 0;
+ z-index: 1001;
+ width: 100%;
+ opacity: 0.75;
+ background-color: #111;
+}
+.flash span {
+ float: left;
+ margin-right: .7em;
+}
+.flash .ui-corner-all {
+ width: 300px;
+ margin: 50px auto 0 auto;
+ padding: 0 5px;
+ opacity: 0.9;
+ font-weight: bold;
+ -moz-box-shadow: 0 0 8px #111;
+ -webkit-box-shadow: 0 0 8px #111;
+ box-shadow: 0 0 8px #111;
+}
View
27 public/stylesheets/style.less
@@ -191,3 +191,30 @@ ul.toolbar a {
.page p {
margin: 10px 0;
}
+
+.flash {
+ position: absolute;
+ top: 0;
+ bottom: 0;
+ z-index: 1001;
+ width: 100%;
+ opacity: 0.75;
+ background-color: #111;
+}
+
+.flash span {
+ float: left;
+ margin-right: .7em;
+}
+
+.flash .ui-corner-all {
+ width: 300px;
+ margin: 50px auto 0 auto;
+ padding: 0 5px;
+ opacity: 0.9;
+ font-weight: bold;
+ -moz-box-shadow: 0 0 8px #111;
+ -webkit-box-shadow: 0 0 8px #111;
+ box-shadow: 0 0 8px #111;
+}
+
View
3  views/layout.jade
@@ -13,9 +13,10 @@ html
ul
li
h1
- a(href='/') Nodepad
+ a(href='/') #{nameAndVersion(appName, version)}
- if (typeof currentUser !== 'undefined')
li.right
a.destroy(href='/sessions') Log Out
+ #{flashMessages}
!= body
script(type='text/javascript', src='/javascripts/application.js')
Please sign in to comment.
Something went wrong with that request. Please try again.