Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require([
- "dijit/registry",
- "dojo/request",
- "dojo/Deferred"
- ], function(registry, request, Deferred) {
- // NOTE: This is written as if it is not a module, but your main JS file that gets executed on page load...
- // checkPassword would NOT be a global method so you would be calling it from some sibling code inside this file...
- function checkPassword(password) {
- var dfd = new Deferred(); // Create a deferred object that we will resolve at a later time
- console.log("c1: inside checkPassword()");
- request.post("checkPwd.php", { // Make XHR Post request to checkPwd.php
- handleAs: "json",
- data: {
- 'user': registry.byId("username").get("value"), // accessing .value isnt the best practice either, should almost always use the getter/setter
- 'pwd': password
- }
- }).then(function(data) { // Our XHR request completed... lets check the status and see if its a success or a failure
- console.log("c2: checkPassword() response", data);
- if (data.status == "true") {// Success!
- console.log ("true");
- dfd.resolve(data); // Resolve our deferred object with the data from the XHR response incase we want to expose the results from the promise
- } else { // Failure!
- console.log ("false");
- 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
- }
- });
- return dfd.promise; // Return the deferred's promise which is then'able.
- }
- // 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
- console.log("m1: before checkPassword()");
- // NOTE: To use this method you would then do something like this... and since we used resolve and reject you could do this
- checkPassword(yourPass).then(logThemIn, kickThemOut); // What this means is if checkPassword success, pass its data to LogThemIn() else pass the failure data to kickThemOut()
- console.log("m2: after checkPassword()");
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement