Registering Callbacks with then

$.ajax returns a Deferred, which has a then method. The then method registers a callback that is run when the Deferred is resolved. When the callback is invoked, it is passed the value sent back in the server response (Figure 14.3).

Figure 14.3  Deferred object invokes callbacks registered with then

Deferred object invokes callbacks registered with then

Start with a simple usage of then. In main.js, your submit handler calls createOrder and addRow. Change this so that addRow is registered as a callback of createOrder.

Open main.js and update the call to formHandler.addSubmitHandler. Chain a .then to the invocation of createOrder. Pass it a callback that runs checkList.addRow.

...
  formHandler.addSubmitHandler(function (data) {
    myTruck.createOrder.call(myTruck, data);
      .then(function () {
        checkList.addRow.call(checkList, data);
      });
  });
...

Instead of invoking addRow immediately after createOrder, you are making addRow dependent on createOrder completing without errors or exceptions.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.12.136.186