Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2014
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 KB | None | 0 0
  1. $scope.createProduct = function() {
  2.  
  3. // validate the formData (using our exentions.js .filter) to make sure that something is there
  4. //if form is empty, nothing will happen
  5. if (!isEmptyObjectFilter($scope.formData)) {
  6.  
  7. // call the create function from our service (returns a promise object)
  8. Emotions.create($scope.formData)
  9.  
  10. // if successful creation, call our get function to get all the new emotions
  11. .success(function(data) {
  12. $scope.formData = {}; // clear the form so our user is ready to enter another
  13. $scope.products = data; // assign our new list of emotions
  14. })
  15. .error(function(data) {
  16. console.log('Error: ' + data);
  17. });
  18. }
  19. };
  20.  
  21. } else if(typeof doc.position == "number") {
  22.  
  23. console.log('yes, a number');
  24.  
  25. // check if there is an existing document with the same position
  26. // use mongoose.model to fetch the model because the model is not compiled yet
  27. mongoose.model("Product").where({_id: {$ne: doc._id}, position: doc.position}).count( function (err, count) {
  28.  
  29. // if there was an error, pass it to next()
  30. if(err)
  31. return next(err);
  32.  
  33. // if there is a doc with the same position, execute an update to move down all the $gte docs
  34. if(count > 0) {
  35. // use mongoose.model to fetch the model because the model is not compiled yet
  36. mongoose.model("Product").update({position: {$gte: doc.position}}, {position: {$inc: 1}}, {multi: 1}, function(err, numAffected) {
  37.  
  38. console.log(numAffected);
  39.  
  40. // Call next() (with or without an error)
  41. next(err);
  42. });
  43.  
  44. } else {
  45. // there are no docs that need to move down, so call next()
  46. next();
  47. }
  48. });
  49.  
  50. var ProductSchema = mongoose.Schema({
  51. title : String,
  52. position: Number
  53. });
  54.  
  55. // before validation starts, the number of Products is counted
  56. // afterwards, the position is set
  57. ProductSchema.pre("validate", function(next) {
  58.  
  59. var doc = this;
  60.  
  61. // if 'position' is not filled in, fill it in
  62. // not using !position because 0 might be a valid value
  63. if(typeof doc.position !== "number") {
  64. // count the number of Products *
  65. // use mongoose.model to fetch the model..because it's not compiled yet
  66. mongoose.model("Product").count(function(err, num) {
  67. // if there was an error, pass it to next()
  68. if(err)
  69. return next(err);
  70.  
  71. // set the position, then call next();
  72. doc.position = num;
  73. return next();
  74. });
  75.  
  76. } else {
  77. // there is no need to count or update positions, so call next()
  78. next();
  79. }
  80. });
  81.  
  82. module.exports = mongoose.model('Product', ProductSchema);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement