Step 4: Adding Products to the Shopping Bag
Putting things in the shopping bag is easy. Users need only click the
affectionately titled link “Gimme One.” This calls the
correspondingly named function gimmeOne(). Lines
112-133 have the details:
function gimmeOne() {
if (!gimmeControl) {
alert("Nothing on this screen to give you.");
return;
}
for (var i = 0; i < shoppingBag.things.length; i++) {
if (categorySet[curCLoc].prodLine[curPLoc].plu ==
shoppingBag.things[i].plu) {
alert("That's already in your bag. You can change the quantity " +
"by choosing View/Change Bag.");
return;
}
}
shoppingBag.things[shoppingBag.things.length] =
categorySet[curCLoc].prodLine[curPLoc];
shoppingBag.things[shoppingBag.things.length - 1].itemQty = 1;
shoppingBag.things[shoppingBag.things.length - 1].category =
categorySet[curCLoc].name;
alert("OK. You put the " +
shoppingBag.things[shoppingBag.things.length - 1].name +
" in your bag.");
}
The first thing gimmeOne() does is to ensure that
there is actually something on the screen to put in the shopping bag.
Variable gimmeControl is set to true immediately
before a product is displayed. Otherwise, any other functions
displaying information on the screen set gimmeControl
to false. Therefore, if
gimmeControl is false, there
is no product on the screen. The user is alerted, and
gimmeOne() returns. Otherwise,
gimmeOne() iterates through the elements of the
things array, which is a property of the
user’s shoppingBag
object to check whether the product ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access