Skip to content

Commit

Permalink
Cleaning out ajax.
Browse files Browse the repository at this point in the history
  • Loading branch information
MrMaksimize committed Mar 7, 2014
1 parent 05de0b3 commit d08208b
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 268 deletions.
59 changes: 1 addition & 58 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ var passportConf = require('./config/passport');
var app = express();
var http = require('http');
var server = http.createServer(app);
var io = require('socket.io').listen(server);

/**
* Mongoose configuration.
Expand Down Expand Up @@ -102,7 +101,6 @@ app.use(express.errorHandler());
*/

app.get('/', homeController.index);
app.get('/note/new', noteController.getNewNoteForm);
app.post('/note/new', noteController.postNewNoteForm);
app.get('/notes/:skip/:limit', noteController.getNotes);
app.get('/login', userController.getLogin);
Expand Down Expand Up @@ -173,61 +171,6 @@ app.get('/auth/venmo/callback', passport.authorize('venmo', { failureRedirect: '
/**
* Start Express server.
*/

//app.listen(app.get('port'), function() {
server.listen(app.get('port'), function() {
app.listen(app.get('port'), function() {
console.log("✔ Express server listening on port %d in %s mode", app.get('port'), app.settings.env);
});

io.configure(function() {
//io.set('transports', ['websocket']);
//io.set("transports", ["xhr-polling"]);
//io.set("polling duration", 10);
});

io.sockets.on('connection', function(socket) {
console.log('SERVER: Connection Made');

// @TODO should be this AJAX?
// Handle submission of a new note on the form;
socket.on('newNoteSubmitted', function(noteData) {
// @TODO use local socket to react differently for submitter.
noteController.saveNote(noteData, function(err, savedNote) {
if (err) {
console.log(err);
var flashErrors = [];
var errorMessage = "Something happend and we couldn't save your note. Please try again.";
if (err.code == 11000) {
console.log('Dup Note.');
flashErrors.push({ type: 'danger', message: "A note like this already exists. Be creative!" });
}
if (err.name == 'ValidationError') {
for (var errorPath in err.errors) {
var error = err.errors[errorPath];
console.log(error);
flashErrors.push({
type: 'danger',
message: error.message
});
}
}
socket.emit('appFlash', flashErrors);
}
else {
io.sockets.emit('newNoteSaved', savedNote);
}
});
});

// Handle onScroll event when user scrolls to bottom of page.
socket.on('additionalNotesRequested', function(requestParams) {
console.log(requestParams);
console.log('Give me new notes');
noteController.getNotes(requestParams).then(function(newNotes) {
console.log('notes promise.');
// Emit new socket with retrieved notes.
socket.emit('additionalNotesLoaded', {notes: newNotes, requestParams: requestParams});
});
});

});
47 changes: 6 additions & 41 deletions controllers/note.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,6 @@ var _ = require('underscore');
var Note = require('../models/Note');


/**
* Abstractions to Node Saving
* Let's abstract the operations we have going on with Mongo because we may have different triggers for
* them.
*/

/**
* GET /note/new
* Signup page.
*/

exports.getNewNoteForm = function(req, res) {
if (req.user) return res.redirect('/');
res.render('new_note', {
title: 'Create Note'
});
};

/**
* POST /note/new
* Create a new local account.
Expand All @@ -29,38 +11,21 @@ exports.getNewNoteForm = function(req, res) {
*/

exports.postNewNoteForm = function(req, res, next) {
req.assert('noteText', 'Note must be between 5 and 150 characters').len(5, 150);

req.assert('noteText', 'Text must be between 1 and 140 characters').len(1, 140);
req.assert('twitterHandle', 'Twitter handle must be between 1 and 25 characters').len(1, 25);
var errors = req.validationErrors();

console.log(req.body);
if (errors) {
req.flash('errors', errors);
return res.redirect('/note/new');
return res.send({ errors: errors });
}

var note = new Note({
text: req.body.noteText,
twitterHandle: req.body.twitterHandle
}).save(function(err, newNote) {
return res.send(newNote);
});

note.save(function(err) {
if (err) {
req.flash('errors', { msg: 'Something happened idk what' });
}
return res.redirect('/');
});

};

// @TODO cleanup the stupid property naming and make it consistent.
exports.saveNote = function(noteData, callback) {
var note = new Note(noteData);
// I suck. this is where controller logic should go and instead its in app. boo.
note.save(function(err, note) {
callback(err, note);
});

};

/**
* GET /notes/:skip/:limit
Expand Down
13 changes: 3 additions & 10 deletions models/Note.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,16 @@ var noteSchema = new mongoose.Schema({

noteSchema.pre('save', function(next) {
// Clean Up Twitter Handle As Needed;
//if (this.twitterHandle.indexOf('@') !== -1) {
// }
if (this.twitterHandle.indexOf('@') !== -1) {
this.twitterHandle = this.twitterHandle.replace("@", "");
}
if (!this.updated) this.updated = new Date;
next();
});

//module.exports = mongoose.model('Note', noteSchema);
var noteModel = mongoose.model('Note', noteSchema);

noteModel.schema.path('twitterHandle').validate(function(value) {
return (value.length > 0 && value.length < 15);
}, 'Please Check Twitter Handle');

noteModel.schema.path('text').validate(function(value) {
return (value.length > 0 && value.length < 140);
}, 'Note must be between 0 and 140 characters in length.');

// If I want to add extra functionality to the model, that's the way to do it.
noteModel.hello = function() {
};
Expand Down
Loading

0 comments on commit d08208b

Please sign in to comment.