Section 3.33. Updating orderCoffee()


3.33. Updating orderCoffee()

Since sendRequest() handles requesting that a coffee maker start brewing an order, you only need to pass in the right request object with orderCoffee(). That means just making two small changes:

 Make both of these changes to your version of orderCoffee(), in coffee.js. var status = getText(coffeemakerStatusDiv1); if (status == "Idle") {   replaceText(coffeemakerStatusDiv1,               "Brewing " + name + "'s " +               size + " " + beverage);   document.forms[0].reset();   var url = "coffeemaker.php?name=" + escape(name) +                            "&size=" + escape(size) +                            "&beverage=" + escape(beverage) +                            "&coffeemaker=1";   sendRequest(If we're sending an order to the first coffee   maker, we always want to use the request1 object.request1, url); } else {   var coffeemakerStatusDiv2 =      document.getElementById("coffeemaker2-status");   status = getText(coffeemakerStatusDiv2);   if (status == "Idle") {     replaceText(coffeemakerStatusDiv2,                 "Brewing " + name + "'s " +                 size + " " + beverage);     document.forms[0].reset();     var url = "coffeemaker.php?name=" + escape(name) +                               "&size=" + escape(size) +                               "&beverage=" + escape(beverage) +                               "&coffeemaker=2";     sendRequestWe always use request2 for requests to the second     coffee maker.(request2, url);   } else {     alert("Sorry! Both coffee makers are busy. " +           "Try again later.");   } 

coffee.js

Just Do It

You're almost done with your multi-request coffee-making wonder app... all that's left is to change the serveDrink() callback so that it can handle two request objects, instead of just one. We've gotten started on this code by filling in all the JavaScript to handle the first request object. Your job is to finish off the callback by writing the code to handle dealing with the second request object.

 function serveDrink() {   if (request1.readyState == 4) {The first half   of this code all deals with the "request1" object now.     if (request1.status == 200) {       var response = request1.responseText;       var whichCoffeemaker = response.substring(0, 1);       var name = response.substring(1, response.length);       if (whichCoffeemaker == "1") {         var coffeemakerStatusDiv1 =           document.getElementById("coffeemaker1-status");         replaceText(coffeemakerStatusDiv1, "Idle");       } else {         var coffeemakerStatusDiv2 =           document.getElementById("coffeemaker2-status");         replaceText(coffeemakerStatusDiv2, "Idle");      }      alert(name + ", your coffee is ready!");      request1 = createRequest();We      have to be sure to "clear" the request object once we're done... this does      that by re-creating the request object.    } else      alert("Error! Request status is " + request1.status);  } else if (request2.readyState == 4) {    // All your code goes hereYour    job is to write all the code that goes in here.  } } 





Head Rush Ajax
Head Rush Ajax (Head First)
ISBN: 0596102259
EAN: 2147483647
Year: 2004
Pages: 241

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net