Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This could fit your needs even i'm not sure to understand fully your problematic here.</p> <p>The important point here seems to keep order on ajax method callbacks. You could do something like that:</p> <p>Create these methods:</p> <pre><code> _nextCall: function (deferreds, method) { if (deferreds.length) this._when(deferreds, method); else console.log(method + " SUCCESS"); }, _when: function (calls, method) { var $promise = $.when(this[calls[0]]()) $promise.then(function () { calls.splice(0, 1); this._nextCall(calls, method); }, function () { console.log(method + " FAILED on: " + calls[0]); }); }, </code></pre> <p>Use it like that e.g:</p> <pre><code>logIn: function logIn() { var calls = ["lockDevice", "getCardLogin", "releaseDevice"]; this._when(calls, arguments.callee.name); }, getPinOnly: function getPinOnly() { var calls = ["getPin"]; this._when(calls, arguments.callee.name); }, </code></pre> <p><a href="http://jsfiddle.net/7x5bw/" rel="nofollow">DEMO</a></p> <p>COMPLETE CODE:</p> <pre><code>var CardReader = { //////////////////// // Different possible messages to the Card Reader //////////////////// lockDevice: function () { return this.makeAjaxCall("GET", "/LockDevice", this.lockDeviceCallback); }, getCardLogin: function () { return this.makeAjaxCall("POST", "/getCardLogin", this.getCardLoginCallback); }, getPin: function () { return this.makeAjaxCall("POST", "/getPin", this.getPinCallback); }, releaseDevice: function () { return this.makeAjaxCall("POST", "/Release", this.releaseDeviceCallback); }, ////////////////// // Callbacks for each message to the Card Reader ////////////////// lockDeviceCallback: function (jqXHR, textStatus) { console.log("lockDeviceCallback"); if (textStatus !== "success") { return; } }, getCardLoginCallback: function (jqXHR, textStatus) { console.log("getCardLoginCallback"); if (textStatus !== "success") { return; } }, getPinCallback: function (jqXHR, textStatus) { console.log("getPinCallback"); if (textStatus !== "success") { return; } }, releaseDeviceCallback: function (jqXHR, textStatus) { console.log("releaseDeviceCallback"); if (textStatus !== "success") { return; } //End }, //////////////// // Other methods //////////////// init: function () { // UI BIndings $('#btn_login').on("click", $.proxy(this.logIn, this)); $('#btn_getPinCallback').on("click", $.proxy(this.getPinOnly, this)); }, _nextCall: function (deferreds, method) { if (deferreds.length) this._when(deferreds, method); else console.log(method + " SUCCESS"); }, _when: function (calls, method) { var $promise = $.when(this[calls[0]]()) $promise.then(function () { calls.splice(0, 1); this._nextCall(calls, method); }, function () { console.log(method + " FAILED on: " + calls[0]); }); }, logIn: function logIn() { var calls = ["lockDevice", "getCardLogin", "releaseDevice"]; this._when(calls, arguments.callee.name); }, getPinOnly: function getPinOnly() { var calls = ["getPin"]; this._when(calls, arguments.callee.name); }, makeAjaxCall: function (callType, resource, callbackMethod) { return $.ajax({ type: callType, url: "/echo/json", // + resource, cache: false, dataType: "json", contentType: "application/json", context: this, success: callbackMethod }); } }; CardReader.init(); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload