Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions lessons/05-client-jquery-ajax/homework/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "homework",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"body-parser": "^1.15.0",
"cookie-parser": "^1.4.1",
"express": "^4.13.4",
"express-handlebars": "^3.0.0",
"mongoose": "^4.4.4",
"morgan": "^1.7.0"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,20 @@ var onError = function(data, status) {

$form.submit(function(event) {
event.preventDefault();
formData = $form.serialize();
// var formData = $form.serialize(); // if this works with how you've set up the form, awesome -- otherwise, the .find(...).val() method below works too

var name = $form.find("[name='name']").val();
var price = $form.find("[name='price']").val();
var inStock = $form.find("[name='inStock']").val();
var customerName = $form.find("[name='customerName']").val();
var inStock = $form.find("[name='inStock']").val();
var customerName = $form.find("[name='customerName']").val();
var price = $form.find("[value ='customerName']").val();

formData = {
var formData = {
name: name,
price: price,
inStock: inStock
}

});
$.get("getIngredient", formData)
.done(onSuccess)
.error(onError);
Expand All @@ -42,7 +41,7 @@ $form.submit(function(event) {
//////////////////////////////////////
/*var $inStock;
var $outOfStock;
var $editIngr;
var $editIngr;
var $addIngr;
*/

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var onSuccess = function(data, status) {
//var img = "<img src='"+data+"'/>";
if (!data) {
alert("Error!");
} else {
} else {
$("#result").html();
}

Expand All @@ -21,8 +21,8 @@ $form.submit(function(event) {

var name = $form.find("[name='name']").val();
var price = $form.find("[name='price']").val();
var inStock = $form.find("[name='inStock']").val();
var customerName = $form.find("[name='customerName']").val();
var inStock = $form.find("[name='inStock']").val();
var customerName = $form.find("[name='customerName']").val();
var price = $form.find("[value ='customerName']").val();

formData = {
Expand Down Expand Up @@ -100,4 +100,4 @@ $ingredients.click(function () {
.done(onSuccess)
.error(onError);
});
*/
*/
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
// I'm going to focus my comments on this file, and the others will be similar.
var $order = $("form.orderForm");
var $ingredient = $("input.options");

var onSuccess = function (data, status) {
var ingredientList = [];
var sumTotal = 0
var orderToSubmit
// Usually it's best practice to put global variables at the top of a file

// I'm giving the success & error handlers unique names so they don't conflict with the similar handlers in other javascript files...
// clientside JavaScript globals aren't scoped to files, they're scoped to all of your clientside js (not like Python)
var orderOnSuccess = function (data, status) {
// The success callback is a good place to reset the global variables -- before you had them "after" you bound the $order.submit handler,
// but JavaScript files only execute sequentially when your browser loads an HTML page (because they're linked in <script> tags in your main.handlebars layout).
// If you want something to happen "after" a submit, the success callback is where it should go.
ingredientList = [];
sumTotal = 0;

if (!data) {
alert("Error!");
Expand All @@ -10,29 +23,25 @@ var onSuccess = function (data, status) {
}
}

var onError = function(data, status) {
var orderOnError = function(data, status) {

console.log("status", status);
console.log("error", data);
};


var ingredientList = [];
var sumTotal = 0
var orderToSubmit


$order.submit(function (event) {

event.preventDefault();
var name = $order.find("[name='customerName']").val();
var ingredients = $('input:checkbox:checked').map(function () { return this.name}).get();
console.log(ingredients.length);
console.log(ingredients);
var ingredients = $('input:checkbox:checked').map(function () { return this.name}).get(); // nice use of functional tools!
var price = $('input:checkbox:checked').map(function () { return this.value}).get();

for (var i = 0; i < price.length; i++) {
sumTotal += price[i]
};
}

$.post('/orders/recieved', {
name: name,
Expand All @@ -41,14 +50,11 @@ $order.submit(function (event) {
})


.done(onSuccess)
.error(onError);
.done(orderOnSuccess)
.error(orderOnError);
// This submit handler looks great -- exactly the right way to prevent default action, get info from the form, and make a post request.
});

var ingredientList = [];
var sumTotal = 0
var orderToSubmit

function handleClick(){
var checkbox = event.target;
var price = checkbox.value
Expand All @@ -62,7 +68,7 @@ function handleClick(){
return orderToSubmit
}


// $ingredients.click(function () {
// console.log("we clicked an")
// debugger;
Expand Down
41 changes: 24 additions & 17 deletions lessons/05-client-jquery-ajax/homework/routes/getIngredient.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ var routes = {};
var ingredientText;

function IngredientConstructor(name, price, inStock){
/* I bet you could even make this function return Mongoose ingredientmodels -- something like
return new IngredientModel({
name: name,
price: price,
inStock: inStock
});
*/
var thisIngredientObj= {
name: name,
price: price,
Expand All @@ -13,18 +20,17 @@ function IngredientConstructor(name, price, inStock){
}

var getIngredient = function(ingredientParams, absolute) {
//var ingredientText;
ingredientText = ingredientParams.name + " $" + ingredientParams.price;
console.log(ingredientText);
var ingredientText = ingredientParams.name + " $" + ingredientParams.price;
var newIngredient = new IngredientModel(IngredientConstructor(ingredientParams.name, ingredientParams.price, ingredientParams.inStock=true));
newIngredient.save(function(err){
newIngredient.save(function(err){
if(err){
res.status(500).send("Error")
res.status(500).send("Error"); // nice error handling :)
}

return newIngredient;
})
}

return newIngredient;
});
};
// IngredientConstruct and getIngredient look great!


routes.getIngredientGET = function(req, res) {
Expand All @@ -46,20 +52,21 @@ routes.getIngredientPOST = function(req, res) {

routes.ingredients = function(req, res) {
IngredientModel.find({}, function(err, data) {

var inStockData = [];
var outStockData = [];
data.forEach(function(ingredient) {
var list = ingredient.inStock ? inStockData : outStockData;
var list = ingredient.inStock ? inStockData : outStockData; // nice ternary logic!
list.push(ingredient);
});

var sortedData = {'inStock':inStockData,
'outOfStock':outStockData};

res.render('ingredients', sortedData);
res.render('ingredients', {
'inStock': inStockData,
'outOfStock': outStockData
}); // I kind of like declaring objects inline like this -- nice little space-saving trick. Up to you though :)
});
}
};
// this handler looks great too.


routes.outOfStock = function(req, res) {
Expand All @@ -68,7 +75,7 @@ routes.outOfStock = function(req, res) {
// IngredientModel.update({'_id':_id}, {'inStock':false}, function(err, num, data) {
// res.end(_id);

IngredientModel.update({'_id':_id}, {"$set" : {"array.$.inStock" : false}})
IngredientModel.update({'_id':_id}, {"$set" : {"array.$.inStock" : false}})
//});
}

Expand Down
6 changes: 3 additions & 3 deletions lessons/05-client-jquery-ajax/homework/routes/kitchen.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var IngredientModel = require('../models/ingredientMongoose.js');
var OrderModel = require('../models/orderMongoose.js');

var routes = {};

routes.showOrders = function (req, res) {
Expand All @@ -10,7 +10,7 @@ routes.showOrders = function (req, res) {
res.status(500).send("Error no orders found");
} else {
if (orders.length > 0) {
res.render("kitchen", {"message": " Orders to fill:", orders});
res.render("kitchen", {"message": " Orders to fill:", orders: orders});
} else {
res.render("kitchen", {"message": "All orders completed"});
}
Expand All @@ -28,4 +28,4 @@ routes.completeOrder = function (req, res) {
})
}

module.exports = routes;
module.exports = routes;