Browse files

fixed a few bugs and made the modularization more standard

  • Loading branch information...
1 parent 2247846 commit 4d5e20301cf5f56a3edebdba49ee55eedfc9d273 @jettro committed Apr 17, 2011
Showing with 51 additions and 27 deletions.
  1. +11 −0 app-socketio.js
  2. +4 −11 app.js
  3. +18 −8 blog.js
  4. BIN public/favicon.ico
  5. +18 −6 site.js
  6. +0 −2 views/blog/blog.jade
View
11 app-socketio.js
@@ -1,3 +1,14 @@
+/**
+ * Used to start the sample application while making use of raw socket.io. No special libraries are used to wrap
+ * socket.io functionality.
+ *
+ * This is just a small wrapper to load the socket.io specific things. The generic application code is available in the
+ * app.js file that is included as well.
+ *
+ * The application is started with an implementation for the chat functionality. In the socket.io case, chat is
+ * implemented using the socketio.js file or module.
+ *
+ */
var Socketio = require('./socketio');
var socketio = new Socketio();
View
15 app.js
@@ -3,18 +3,17 @@ function App() {
var chat;
var properties = require('./properties');
-
+var Site = require('./site');
+var Blog = require('./blog');
App.prototype.start = function(chat) {
var pub = __dirname + '/public';
var express = require('express')
, app = express.createServer()
- , blog = require('./blog');
+ , blog = new Blog()
+ , site = new Site(properties);
- var site = require('./site');
- site.init(properties);
-
app.configure(function() {
app.set('view engine', 'jade');
app.set('views', __dirname + '/views');
@@ -28,12 +27,6 @@ App.prototype.start = function(chat) {
app.use(express.session({ secret: "keyboard cat" }));
});
- app.dynamicHelpers({
- current_user: function(req) {
- return req.session.user;
- }
- });
-
// general
app.get('/', site.index);
app.get('/authenticate', site.authenticate);
View
26 blog.js
@@ -1,21 +1,31 @@
+function Blog() {}
+
var ArticleProvider = require('./articleprovider-memory').ArticleProvider;
var articleProvider = new ArticleProvider();
-
-exports.index = function(req, res) {
+Blog.prototype.index = function(req, res) {
articleProvider.findAll(function(error, docs) {
- res.render('blog/blog', {locals: {docs: docs}});
+ res.render('blog/blog', {locals: {docs: docs, loginName:obtainLoginName(req)}});
});
};
-
-exports.blogShowForm = function (req, res) {
- res.render('blog/blog_new', {locals: {title: 'New Blog Item'}});
+Blog.prototype.blogShowForm = function (req, res) {
+ res.render('blog/blog_new', {locals: {title: 'New Blog Item', loginName:obtainLoginName(req)}});
};
-exports.blogPostForm = function (req, res) {
+Blog.prototype.blogPostForm = function (req, res) {
console.log('The provided title is : %s', req.body.new_title);
articleProvider.save({title: req.body.new_title, body: req.body.new_body},
function(error, docs) {
res.redirect('/blog');
});
-};
+};
+
+function obtainLoginName(req) {
+ var loginName = '';
+ if (req.session.oauth && req.session.user) {
+ loginName = req.session.user.name;
+ }
+ return loginName;
+}
+
+module.exports = Blog;
View
BIN public/favicon.ico
Binary file not shown.
View
24 site.js
@@ -1,22 +1,32 @@
+/**
+ * The site module is initialized using a properties object. This objects needs to have the following parameters:
+ * - consumer.key
+ * - consumer.secret
+ * - host
+ *
+ * The key and secret are used to connect to twitter for authentication. The host is used for the callback for twitter.
+ *
+ */
+
var consumer_key;
var consumer_secret;
var host;
-exports.init = function(properties) {
+function Site(properties) {
consumer_key = properties.consumer.key;
consumer_secret = properties.consumer.secret;
host = properties.host;
-};
+}
-exports.index = function(req, res) {
+Site.prototype.index = function(req, res) {
var loginName = '';
if (req.session.oauth && req.session.user) {
loginName = req.session.user.name;
}
res.render('index', {locals: {loginName:loginName}});
};
-exports.authenticated = function(req, res, next) {
+Site.prototype.authenticated = function(req, res, next) {
if (req.session.oauth) {
req.session.oauth.verifier = req.query.oauth_verifier;
var oauth = req.session.oauth;
@@ -33,7 +43,7 @@ exports.authenticated = function(req, res, next) {
}
};
-exports.authenticate = function(req, res) {
+Site.prototype.authenticate = function(req, res) {
var sys = require('sys');
var OAuth = require('oauth').OAuth;
oa = new OAuth("https://api.twitter.com/oauth/request_token",
@@ -50,4 +60,6 @@ exports.authenticate = function(req, res) {
res.redirect('https://api.twitter.com/oauth/authenticate?oauth_token=' + oauth_token);
}
});
-};
+};
+
+module.exports = Site;
View
2 views/blog/blog.jade
@@ -2,8 +2,6 @@ div#body
h1 My blog
div
span
- a(href='/') Home
- span
a(href='/blog/new') New blog item
div#articles
- each blog in docs

0 comments on commit 4d5e203

Please sign in to comment.