Permalink
Browse files

Validators are now working but I'm having a pain with ObjectIds

  • Loading branch information...
1 parent 5ad614b commit 6bc1a5342c5e40b5512d66bde1f3fb9360fdaf5a @tbergeron committed Apr 17, 2012
Showing with 24 additions and 13 deletions.
  1. +0 −2 modules/app/controllers/home.js
  2. +17 −11 modules/app/repositories/Projects.js
  3. +3 −0 modules/dependency.json
  4. +4 −0 modules/libs/helpers.js
@@ -1,7 +1,5 @@
module.exports = {
index: function(req, res) {
- this.helpers.flash(req, 'error', 'Testing messages');
-
res.render('index', {
title: 'ThinAir'
});
@@ -29,41 +29,47 @@ var Projects = createRepository('projects', {
// saves a project
save: function(project, callback) {
- var project_code = this.helpers.slugify(project.name);
+ var project_code = this.helpers.slugify(project.name),
+ that = this;
//todo: CHECK FOR DUPLICATES
this.validator.validate('project', project, function(errors) {
+ console.log(errors);
if (!errors) {
- if (this.helpers.isNew(project)) {
+ if (that.helpers.isNew(project)) {
//if it's a new project, save it
- this.save({ code: project_code, name: project.name });
+ that.save({ code: project_code, name: project.name });
//fetch the saved project
- this.findOne({ code: project_code }, function(err, new_project) {
+ that.findOne({ code: project_code }, function(err, new_project) {
return callback(new_project);
});
} else {
//if it's not a new project, update the existing one
- this.update({ _id: db.ObjectId(project._id) }, {
+ that.update({ _id: project._id }, {
$set: { code: project_code, name: project.name }
});
//fetch the created project
- this.findOne({ _id: db.ObjectId(project._id) }, function(err, updated_project) {
+ that.findOne({ _id: project._id }, function(err, updated_project) {
return callback(updated_project);
});
}
} else {
// todo: remove this when validations are done
var errors = null;
//if there's validation errors
- if (this.helpers.isNew(project)) {
+ if (that.helpers.isNew(project)) {
//if it's a new project, send it as it is
return callback(project, errors);
} else {
//if it's an existing one, fetch it and put and replace the values with the on from the form
- this.findOne({ _id: db.ObjectId(project._id) }, function(err, fetched_project) {
- fetched_project.code = project_code;
- fetched_project.name = project.name;
- return callback(fetched_project, errors);
+ that.findOne({ _id: project._id }, function(err, fetched_project) {
+ if (fetched_project) {
+ fetched_project.code = project_code;
+ fetched_project.name = project.name;
+ return callback(fetched_project, errors);
+ } else {
+ return callback(null, errors);
+ }
});
}
}
@@ -11,6 +11,9 @@
"./libs/configure.js": {
"helpers": "./libs/helpers.js"
},
+ "./libs/validator.js": {
+ "helpers": "./libs/helpers.js"
+ },
"./app/routes.js": {
"router": "./libs/router.js",
"controllers": ["./app/controllers/"]
@@ -58,6 +58,10 @@ var Helpers = {
}
},
+ isNew: function(object) {
+ return (object._id.length > 0) ? false : true;
+ },
+
// todo: it would be cool to eventually do something with this
registerHandlebarsHelpers: function(hbs) {
// hbs.registerHelper('fullName', function(person) {

0 comments on commit 6bc1a53

Please sign in to comment.