Advertisement
Guest User

Untitled

a guest
Feb 27th, 2015
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. require([
  2.     "dijit/registry",
  3.     "dojo/request",
  4.     "dojo/Deferred"
  5. ], function(registry, request, Deferred) {
  6.  
  7. // NOTE: This is written as if it is not a module, but your main JS file that gets executed on page load...
  8. //       checkPassword would NOT be a global method so you would be calling it from some sibling code inside this file...
  9.  
  10. function checkPassword(password) {
  11.     var dfd = new Deferred();   // Create a deferred object that we will resolve at a later time
  12.     console.log("c1: inside checkPassword()");
  13.     request.post("checkPwd.php", {  // Make XHR Post request to checkPwd.php
  14.         handleAs: "json",
  15.         data: {
  16.             'user': registry.byId("username").get("value"), // accessing .value isnt the best practice either, should almost always use the getter/setter
  17.             'pwd': password
  18.         }
  19.     }).then(function(data) {        // Our XHR request completed... lets check the status and see if its a success or a failure
  20.         console.log("c2: checkPassword() response", data);
  21.         if (data.status == "true") {// Success!
  22.             console.log ("true");
  23.             dfd.resolve(data);      // Resolve our deferred object with the data from the XHR response incase we want to expose the results from the promise
  24.         } else {                    // Failure!
  25.             console.log ("false");
  26.             dfd.reject(data);       // Reject our deferred object again with the data from the XHR response so that you could tailor an error message or something from it
  27.         }
  28.     });
  29.  
  30.     return dfd.promise;     // Return the deferred's promise which is then'able.
  31. }
  32.  
  33. // For demo purposes I have added a console message in each stage of the Async process and I have numbered them in the order they are written in code just to help illustrate the round about way Async happens c# are generated by code in checkPassword and m# are from the code surrounding the checkPassword call
  34.  
  35. console.log("m1: before checkPassword()");
  36. // NOTE: To use this method you would then do something like this... and since we used resolve and reject you could do this
  37. checkPassword(yourPass).then(logThemIn, kickThemOut);   // What this means is if checkPassword success, pass its data to LogThemIn() else pass the failure data to kickThemOut()
  38. console.log("m2: after checkPassword()");
  39. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement