O'Reilly logo

JavaScript Application Cookbook by Jerry Bradenbaugh

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required