pwCheck function in PasswordValidator

classic Classic list List threaded Threaded
21 messages Options
12
Reply | Threaded
Open this post in threaded view
|

pwCheck function in PasswordValidator

Harry Devine
When is this function called?  Now that I see how to use the
PasswordValidator control on a basic level, I wanted to see if I could
re-engineer some of  my older custom password checking functions.  I
have a page with the typical old/new/verify boxes.  I see that the Old
PasswordValidator box always gives me an "Invalid Password" validation
error when I'm typing.  I see in the example that there's a pwCheck
functon, but when does that get called?  Is it safe to have an
overridden function that gets called?

My backend is checking the user against our LDAP server using the PHP
ldap commands, so I'm not really sure I want to a bind for each
character that's entered into the Old password box.

Any thoughts would be greatly appreciated!

Thanks,
Harry
--
Dojo Toolkit: http://dojotoolkit.org/
Tutorials: http://dojotoolkit.org/documentation/

[hidden email]
To unsubscribe, visit: http://mail.dojotoolkit.org/mailman/listinfo/dojo-interest
Reply | Threaded
Open this post in threaded view
|

Re: pwCheck function in PasswordValidator

Harry Devine
OK, so I figured out that it runs after the old password is entered, however, I can't get the value returned.  In my require section, I have the following:
             var bPwdValid;
             var successCallback = function() {
               bPwdValid = true;
             }
             var errorCallback = function() {
               bPwdValid = false;
             }


Here's where I have my dojox/form/PasswordValidator configured:


  <div data-dojo-type="dojox/form/PasswordValidator" name="pwdVerify">
    <script type="dojo/method" data-dojo-event="pwCheck" data-dojo-args="password">
      checkPassword(password, successCallback, errorCallback);
      alert (bPwdValid);

      return bPwdValid;
    </script>
    <b>Old (or Temp) Password:</b>
    <input type="password" pwType="old"><br>
    <b>New Password:</b>
    <input type="password" pwType="new" id="newPwd"><br>
    <b>Confirm Password:</b>
    <input type="password" pwType="verify"><br>
  </div>

My function checkPassword is here:

function checkPassword(password, successCallback, errorCallback) {
  require(["dijit/registry", "dojo/request", "dojo/dom", "dojo/json"], 
    function(registry,request,dom,json) {
    dom.byId("ldapResult").innerHTML = password;
    request.post("checkPwd.php", {
      handleAs: "json",
      data: {'user': registry.byId("username").value,
             'pwd': password} 
    }).then (function(data){
      if (data.status == "true") {
        successCallback();
      }
      else {
        errorCallback();
      }
    });
  });  
}
 
Firebug shows the error "ReferenceError: successCallback is not defined".  If I moved the successCallback and errorCallback functions from the require area to the <script> section where checkPassword is called, the error goes away, but the bPwdValid value is undefined.

How do I get the pwCheck function to actually return the true or false value?

Thanks,
Harry

On 02/24/2015 01:32 PM, Harry Devine wrote:
When is this function called?  Now that I see how to use the 
PasswordValidator control on a basic level, I wanted to see if I could 
re-engineer some of  my older custom password checking functions.  I 
have a page with the typical old/new/verify boxes.  I see that the Old 
PasswordValidator box always gives me an "Invalid Password" validation 
error when I'm typing.  I see in the example that there's a pwCheck 
functon, but when does that get called?  Is it safe to have an 
overridden function that gets called?

My backend is checking the user against our LDAP server using the PHP 
ldap commands, so I'm not really sure I want to a bind for each 
character that's entered into the Old password box.

Any thoughts would be greatly appreciated!

Thanks,
Harry


--
Dojo Toolkit: http://dojotoolkit.org/
Tutorials: http://dojotoolkit.org/documentation/

[hidden email]
To unsubscribe, visit: http://mail.dojotoolkit.org/mailman/listinfo/dojo-interest
Reply | Threaded
Open this post in threaded view
|

Re: pwCheck function in PasswordValidator

Harry Devine
Anyone????  I'm totally at a standstill with this.  If I print out bPwdValid in both callbacks, they are correct.  But when the pwCheck needs to return true or false, its still undefined.

  <div data-dojo-type="dojox/form/PasswordValidator" name="pwdVerify">
    <script type="dojo/method" data-dojo-event="pwCheck" data-dojo-args="password">
      var bPwdValid;
      var successCallback = function() {
        bPwdValid = true;
        console.log(bPwdValid);                    <----------------- Prints true when the xhr has success

      }
      var errorCallback = function() {
        bPwdValid = false;
        console.log(bPwdValid);                    <----------------- Prints false when the xhr has errors


      }
      checkPassword(password, successCallback, errorCallback);
      console.log(bPwdValid);                    <----------------- Prints undefined 
    </script>
    <b>Old (or Temp) Password:</b>
    <input type="password" pwType="old"><br>
    <b>New Password:</b>
    <input type="password" pwType="new" id="newPwd"><br>
    <b>Confirm Password:</b>
    <input type="password" pwType="verify"><br>
  </div>
Please help!
Harry

On 02/25/2015 08:59 AM, Harry Devine wrote:
OK, so I figured out that it runs after the old password is entered, however, I can't get the value returned.  In my require section, I have the following:
             var bPwdValid;
             var successCallback = function() {
               bPwdValid = true;
             }
             var errorCallback = function() {
               bPwdValid = false;
             }


Here's where I have my dojox/form/PasswordValidator configured:


  <div data-dojo-type="dojox/form/PasswordValidator" name="pwdVerify">
    <script type="dojo/method" data-dojo-event="pwCheck" data-dojo-args="password">
      checkPassword(password, successCallback, errorCallback);
      alert (bPwdValid);

      return bPwdValid;
    </script>
    <b>Old (or Temp) Password:</b>
    <input type="password" pwType="old"><br>
    <b>New Password:</b>
    <input type="password" pwType="new" id="newPwd"><br>
    <b>Confirm Password:</b>
    <input type="password" pwType="verify"><br>
  </div>

My function checkPassword is here:

function checkPassword(password, successCallback, errorCallback) {
  require(["dijit/registry", "dojo/request", "dojo/dom", "dojo/json"], 
    function(registry,request,dom,json) {
    dom.byId("ldapResult").innerHTML = password;
    request.post("checkPwd.php", {
      handleAs: "json",
      data: {'user': registry.byId("username").value,
             'pwd': password} 
    }).then (function(data){
      if (data.status == "true") {
        successCallback();
      }
      else {
        errorCallback();
      }
    });
  });  
}
 
Firebug shows the error "ReferenceError: successCallback is not defined".  If I moved the successCallback and errorCallback functions from the require area to the <script> section where checkPassword is called, the error goes away, but the bPwdValid value is undefined.

How do I get the pwCheck function to actually return the true or false value?

Thanks,
Harry

On 02/24/2015 01:32 PM, Harry Devine wrote:
When is this function called?  Now that I see how to use the 
PasswordValidator control on a basic level, I wanted to see if I could 
re-engineer some of  my older custom password checking functions.  I 
have a page with the typical old/new/verify boxes.  I see that the Old 
PasswordValidator box always gives me an "Invalid Password" validation 
error when I'm typing.  I see in the example that there's a pwCheck 
functon, but when does that get called?  Is it safe to have an 
overridden function that gets called?

My backend is checking the user against our LDAP server using the PHP 
ldap commands, so I'm not really sure I want to a bind for each 
character that's entered into the Old password box.

Any thoughts would be greatly appreciated!

Thanks,
Harry





--
Dojo Toolkit: http://dojotoolkit.org/
Tutorials: http://dojotoolkit.org/documentation/

[hidden email]
To unsubscribe, visit: http://mail.dojotoolkit.org/mailman/listinfo/dojo-interest
Reply | Threaded
Open this post in threaded view
|

Re: pwCheck function in PasswordValidator

Jared Jurkiewicz
XHR's are asynchronous, meaning they return/execute later.  Your code is assuming they are synchronous.   You cannot do this:

      checkPassword(password, successCallback, errorCallback);
      console.log(bPwdValid);               

As the xhr will not have run and executed before you reach that console.log.  That's why you have to pass a callback, a function to run LATER when the XHR returns.   You will need to handle all your password logic in those callbacks.  This is basic asynchronous web programming.  You do not want synchronous XHR, as it has tremendous detrimental effects on the web browser/user experience.  Namely, it hangs/locks up the entire browser until it returns. 

Sincerely,
-- Jared Jurkiewicz

--
Dojo Toolkit: http://dojotoolkit.org/
Tutorials: http://dojotoolkit.org/documentation/

[hidden email]
To unsubscribe, visit: http://mail.dojotoolkit.org/mailman/listinfo/dojo-interest
Reply | Threaded
Open this post in threaded view
|

Re: pwCheck function in PasswordValidator

Harry Devine
How do I make this asynchronous then?  I thought I had it that way by sending in the 2 callbacks (successCallback and errorCallback), then calling them accordingly from checkpassword:

function checkPassword(password, successCallback, errorCallback) {
  require(["dijit/registry", "dojo/request", "dojo/dom", "dojo/json"], 
    function(registry,request,dom,json) {
    dom.byId("ldapResult").innerHTML = password;
    request.post("checkPwd.php", {
      handleAs: "json",
      data: {'user': registry.byId("username").value,
             'pwd': password} 
    }).then (function(data){
      if (data.status == "true") {
        successCallback();
      }
      else {
        errorCallback();
      }
    });
  });  
}

If the above is not doing that, can you spot where I'm going wrong?

Thanks,
Harry

On 02/27/2015 10:46 AM, Jared Jurkiewicz wrote:
XHR's are asynchronous, meaning they return/execute later.  Your code is assuming they are synchronous.   You cannot do this:

      checkPassword(password, successCallback, errorCallback);
      console.log(bPwdValid);               

As the xhr will not have run and executed before you reach that console.log.  That's why you have to pass a callback, a function to run LATER when the XHR returns.   You will need to handle all your password logic in those callbacks.  This is basic asynchronous web programming.  You do not want synchronous XHR, as it has tremendous detrimental effects on the web browser/user experience.  Namely, it hangs/locks up the entire browser until it returns. 

Sincerely,
-- Jared Jurkiewicz




--
Dojo Toolkit: http://dojotoolkit.org/
Tutorials: http://dojotoolkit.org/documentation/

[hidden email]
To unsubscribe, visit: http://mail.dojotoolkit.org/mailman/listinfo/dojo-interest
Reply | Threaded
Open this post in threaded view
|

Re: pwCheck function in PasswordValidator

Karl Tiedt
Your function appears to be fine... your logic problem is the console.log() directly after it that Jared highlighted... Comments added for clarification

    checkPassword(password, successCallback, errorCallback);  // Could take 30 seconds to resolve
    console.log(bPwdValid);  // Called immediately after checkPassword is called (NOT resolved)

Move your console.log to your success and errorCallback or have checkPassword return a deferred that you properly resolve or reject and then you can do something like:

checkPassword().then(.....);

-Karl Tiedt

On Fri, Feb 27, 2015 at 7:50 AM, Harry Devine <[hidden email]> wrote:
How do I make this asynchronous then?  I thought I had it that way by sending in the 2 callbacks (successCallback and errorCallback), then calling them accordingly from checkpassword:

function checkPassword(password, successCallback, errorCallback) {
  require(["dijit/registry", "dojo/request", "dojo/dom", "dojo/json"], 
    function(registry,request,dom,json) {
    dom.byId("ldapResult").innerHTML = password;
    request.post("checkPwd.php", {
      handleAs: "json",
      data: {'user': registry.byId("username").value,
             'pwd': password} 
    }).then (function(data){
      if (data.status == "true") {
        successCallback();
      }
      else {
        errorCallback();
      }
    });
  });  
}

If the above is not doing that, can you spot where I'm going wrong?

Thanks,
Harry

On 02/27/2015 10:46 AM, Jared Jurkiewicz wrote:
XHR's are asynchronous, meaning they return/execute later.  Your code is assuming they are synchronous.   You cannot do this:

      checkPassword(password, successCallback, errorCallback);
      console.log(bPwdValid);               

As the xhr will not have run and executed before you reach that console.log.  That's why you have to pass a callback, a function to run LATER when the XHR returns.   You will need to handle all your password logic in those callbacks.  This is basic asynchronous web programming.  You do not want synchronous XHR, as it has tremendous detrimental effects on the web browser/user experience.  Namely, it hangs/locks up the entire browser until it returns. 

Sincerely,
-- Jared Jurkiewicz




--
Dojo Toolkit: http://dojotoolkit.org/
Tutorials: http://dojotoolkit.org/documentation/

[hidden email]
To unsubscribe, visit: http://mail.dojotoolkit.org/mailman/listinfo/dojo-interest



--
Dojo Toolkit: http://dojotoolkit.org/
Tutorials: http://dojotoolkit.org/documentation/

[hidden email]
To unsubscribe, visit: http://mail.dojotoolkit.org/mailman/listinfo/dojo-interest
Reply | Threaded
Open this post in threaded view
|

Re: pwCheck function in PasswordValidator

Harry Devine
Something like this?

<script type="dojo/method" data-dojo-event="pwCheck" data-dojo-args="password">
  checkPassword(password).then(function (data){
    return data.status;
  });
</script>

In my javascript:

function checkPassword(password) {
  require("dijit/registry", "dojo/request", "dojo/Deferred"],
  function(registry, request, Deferred) {
    var deferred = new Deferred();
    request.post("checkPwd.php", {
      handleAs:"json",
      data: {'user': registry.byId("username").value,
                'pwd': password}

    }).then (function(data){
      if (data.status == "true") {
        console.log ("true");
      }
      else {
        console.log ("false");
      }
    });
   return deferred.promise;
  });
}

For full disclosure, this is what I have now, and it doesn't work.  I'm still unclear on when things happen during the async process.

Thanks,
Harry

From: "Karl Tiedt" <[hidden email]>
To: [hidden email]
Sent: Friday, February 27, 2015 1:44:13 PM
Subject: Re: [Dojo-interest] pwCheck function in PasswordValidator

Your function appears to be fine... your logic problem is the console.log() directly after it that Jared highlighted... Comments added for clarification

    checkPassword(password, successCallback, errorCallback);  // Could take 30 seconds to resolve
    console.log(bPwdValid);  // Called immediately after checkPassword is called (NOT resolved)

Move your console.log to your success and errorCallback or have checkPassword return a deferred that you properly resolve or reject and then you can do something like:

checkPassword().then(.....);

-Karl Tiedt


--
Dojo Toolkit: http://dojotoolkit.org/
Tutorials: http://dojotoolkit.org/documentation/

[hidden email]
To unsubscribe, visit: http://mail.dojotoolkit.org/mailman/listinfo/dojo-interest
Reply | Threaded
Open this post in threaded view
|

Re: pwCheck function in PasswordValidator

Karl Tiedt
The whole require() inside of function calls is a horrible pattern IMO but I was more referring to this style:

function checkPassword(password) {
  require("dijit/registry", "dojo/request", "dojo/Deferred"],
  function(registry, request, Deferred) {
    var deferred = new Deferred();
    request.post("checkPwd.php", {
      handleAs:"json",
      data: {'user': registry.byId("username").value,
                'pwd': password}

    }).then (function(data){
      if (data.status == "true") {
        console.log ("true");
deferred.resolve(data);
      }
      else {
        console.log ("false");
deferred.reject(data);
      }
    });
   return deferred.promise;
  });
}

You have to resolve or reject the deferred appropriately...
As a Javascript dev understanding Async/Deferred/Promises is a great thing to nail down ;)


-Karl Tiedt

On Fri, Feb 27, 2015 at 11:04 AM, Harry Devine <[hidden email]> wrote:
Something like this?

<script type="dojo/method" data-dojo-event="pwCheck" data-dojo-args="password">
  checkPassword(password).then(function (data){
    return data.status;
  });
</script>

In my javascript:

function checkPassword(password) {
  require("dijit/registry", "dojo/request", "dojo/Deferred"],
  function(registry, request, Deferred) {
    var deferred = new Deferred();
    request.post("checkPwd.php", {
      handleAs:"json",
      data: {'user': registry.byId("username").value,
                'pwd': password}

    }).then (function(data){
      if (data.status == "true") {
        console.log ("true");
      }
      else {
        console.log ("false");
      }
    });
   return deferred.promise;
  });
}

For full disclosure, this is what I have now, and it doesn't work.  I'm still unclear on when things happen during the async process.

Thanks,
Harry

From: "Karl Tiedt" <[hidden email]>
To: [hidden email]
Sent: Friday, February 27, 2015 1:44:13 PM
Subject: Re: [Dojo-interest] pwCheck function in PasswordValidator

Your function appears to be fine... your logic problem is the console.log() directly after it that Jared highlighted... Comments added for clarification

    checkPassword(password, successCallback, errorCallback);  // Could take 30 seconds to resolve
    console.log(bPwdValid);  // Called immediately after checkPassword is called (NOT resolved)

Move your console.log to your success and errorCallback or have checkPassword return a deferred that you properly resolve or reject and then you can do something like:

checkPassword().then(.....);

-Karl Tiedt


--
Dojo Toolkit: http://dojotoolkit.org/
Tutorials: http://dojotoolkit.org/documentation/

[hidden email]
To unsubscribe, visit: http://mail.dojotoolkit.org/mailman/listinfo/dojo-interest



--
Dojo Toolkit: http://dojotoolkit.org/
Tutorials: http://dojotoolkit.org/documentation/

[hidden email]
To unsubscribe, visit: http://mail.dojotoolkit.org/mailman/listinfo/dojo-interest
Reply | Threaded
Open this post in threaded view
|

Re: pwCheck function in PasswordValidator

Harry Devine
I've never been able to use things like dijit/registry without doing the requires inside of the function.  If there's a better way, I'm more than happy to switch that.  As for your example, that portion worked, as I do get the console.log appropriately.  But I get an error when the checkPassword function is called complaining about then being undefined.

checkPassword(password).then(function(data){    <--------------- Uncaught TypeError: Cannot read property 'then' of undefined
  return data.status;
});

I agree that the async/deferred/promise mechanism would be great to nail down, and obviously due to my questions in this thread, I'm not there yet.  But I'm trying and learning, and your assistance is greatly appreciated!

Harry

From: "Karl Tiedt" <[hidden email]>
To: [hidden email]
Sent: Friday, February 27, 2015 2:32:56 PM
Subject: Re: [Dojo-interest] pwCheck function in PasswordValidator

The whole require() inside of function calls is a horrible pattern IMO but I was more referring to this style:

function checkPassword(password) {
  require("dijit/registry", "dojo/request", "dojo/Deferred"],
  function(registry, request, Deferred) {
    var deferred = new Deferred();
    request.post("checkPwd.php", {
      handleAs:"json",
      data: {'user': registry.byId("username").value,
                'pwd': password}

    }).then (function(data){
      if (data.status == "true") {
        console.log ("true");
deferred.resolve(data);
      }
      else {
        console.log ("false");
deferred.reject(data);
      }
    });
   return deferred.promise;
  });
}

You have to resolve or reject the deferred appropriately...
As a Javascript dev understanding Async/Deferred/Promises is a great thing to nail down ;)


-Karl Tiedt

On Fri, Feb 27, 2015 at 11:04 AM, Harry Devine <[hidden email]> wrote:
Something like this?

<script type="dojo/method" data-dojo-event="pwCheck" data-dojo-args="password">
  checkPassword(password).then(function (data){
    return data.status;
  });
</script>

In my javascript:

function checkPassword(password) {
  require("dijit/registry", "dojo/request", "dojo/Deferred"],
  function(registry, request, Deferred) {
    var deferred = new Deferred();
    request.post("checkPwd.php", {
      handleAs:"json",
      data: {'user': registry.byId("username").value,
                'pwd': password}

    }).then (function(data){
      if (data.status == "true") {
        console.log ("true");
      }
      else {
        console.log ("false");
      }
    });
   return deferred.promise;
  });
}

For full disclosure, this is what I have now, and it doesn't work.  I'm still unclear on when things happen during the async process.

Thanks,
Harry

From: "Karl Tiedt" <[hidden email]>
To: [hidden email]
Sent: Friday, February 27, 2015 1:44:13 PM
Subject: Re: [Dojo-interest] pwCheck function in PasswordValidator

Your function appears to be fine... your logic problem is the console.log() directly after it that Jared highlighted... Comments added for clarification

    checkPassword(password, successCallback, errorCallback);  // Could take 30 seconds to resolve
    console.log(bPwdValid);  // Called immediately after checkPassword is called (NOT resolved)

Move your console.log to your success and errorCallback or have checkPassword return a deferred that you properly resolve or reject and then you can do something like:

checkPassword().then(.....);

-Karl Tiedt


--
Dojo Toolkit: http://dojotoolkit.org/
Tutorials: http://dojotoolkit.org/documentation/

[hidden email]
To unsubscribe, visit: http://mail.dojotoolkit.org/mailman/listinfo/dojo-interest



--
Dojo Toolkit: http://dojotoolkit.org/
Tutorials: http://dojotoolkit.org/documentation/

[hidden email]
To unsubscribe, visit: http://mail.dojotoolkit.org/mailman/listinfo/dojo-interest


--
Dojo Toolkit: http://dojotoolkit.org/
Tutorials: http://dojotoolkit.org/documentation/

[hidden email]
To unsubscribe, visit: http://mail.dojotoolkit.org/mailman/listinfo/dojo-interest
Reply | Threaded
Open this post in threaded view
|

Re: pwCheck function in PasswordValidator

sg3235
Harry…You’re going to have to really think through what’s happening at each line.  Notice that where you are returning the deferred is inside the require call back function.  The checkPassword function has no return value.  Any return from the require call back function is ignored.  That’s the basis of what your problem is below.  But you seem to be missing the async/deferred/promise concept all together.  And require fits right into that async concept.  When you call checkPassword, the callback function to required will not necessarily be called before the call to checkPassword returns.  Assuming you need your function to be globally defined, you would rework your code to something like this:
require ([‘dojo/request’, ‘dojo/Deferred’], function (request, Deferred) {
window.checkPassword = function (password) {
// checkPassword body here…now you can return a Deferred, which will be the result of calling the function, like you have below
};
});

Note that you can’t assume that the checkPassword function is available yet!  If the next line of code after this require was to call checkPassword, it would probably fail.  For this reason, it would be better to define checkPassword as its own AMD module.  Put the following code in a file called checkPassword.js:

define ([‘dojo/request’, ‘dojo/Deferred’], function (request, Deferred) {
function checkPassword (password) {
var p = new Deferred();
// checkPassword body here…now you can return a Deferred, which will be the result of calling the function, like you have below
return p;
};

return checkPassword;
});

Now you would use the function inside a require:

require ([‘./checkPassword’], function (checkPassword) {
checkPassword(password).then(function (data) {
// Handle successful password check here
};
});

Note that you can’t return the status from the then function…that’s trying to drop back into the sync world.  Think about what piece of code is actually invoking the function.  It’s the Deferred that you created in your checkPassword function.  Here are the (simplified) sequence of events:
checkPassword is called.
Deferred is created.
Request is made (but no answer yet!)
Deferred is returned.
Deferred then is called adding a call back function to a list of functions to be called when “resolve” is invoked.  Your “then” function is now in this list

Other stuff happens here…whatever comes next in your code after you invoked checkPassword!!!

Response is received (and handled by a separate Deferred that invokes the “then” method on the request)
Deferred.resolve is called with data
Deferred goes through its list of registered “then” functions and finds the one you attached to checkPassword.
Deferred calls your then function (What does it do with a return value??? I believe your function can return another promise!)

Hope this helps!

Steve

On Feb 27, 2015, at 1:43 PM, Harry Devine <[hidden email]> wrote:

I've never been able to use things like dijit/registry without doing the requires inside of the function.  If there's a better way, I'm more than happy to switch that.  As for your example, that portion worked, as I do get the console.log appropriately.  But I get an error when the checkPassword function is called complaining about then being undefined.

checkPassword(password).then(function(data){    <--------------- Uncaught TypeError: Cannot read property 'then' of undefined
  return data.status;
});

I agree that the async/deferred/promise mechanism would be great to nail down, and obviously due to my questions in this thread, I'm not there yet.  But I'm trying and learning, and your assistance is greatly appreciated!

Harry

From: "Karl Tiedt" <[hidden email]>
To: [hidden email]
Sent: Friday, February 27, 2015 2:32:56 PM
Subject: Re: [Dojo-interest] pwCheck function in PasswordValidator

The whole require() inside of function calls is a horrible pattern IMO but I was more referring to this style:

function checkPassword(password) {
  require("dijit/registry", "dojo/request", "dojo/Deferred"],
  function(registry, request, Deferred) {
    var deferred = new Deferred();
    request.post("checkPwd.php", {
      handleAs:"json",
      data: {'user': registry.byId("username").value,
                'pwd': password}

    }).then (function(data){
      if (data.status == "true") {
        console.log ("true");
deferred.resolve(data);
      }
      else {
        console.log ("false");
deferred.reject(data);
      }
    });
   return deferred.promise;
  });
}

You have to resolve or reject the deferred appropriately...
As a Javascript dev understanding Async/Deferred/Promises is a great thing to nail down ;)


-Karl Tiedt

On Fri, Feb 27, 2015 at 11:04 AM, Harry Devine <[hidden email]> wrote:
Something like this?

<script type="dojo/method" data-dojo-event="pwCheck" data-dojo-args="password">
  checkPassword(password).then(function (data){
    return data.status;
  });
</script>

In my javascript:

function checkPassword(password) {
  require("dijit/registry", "dojo/request", "dojo/Deferred"],
  function(registry, request, Deferred) {
    var deferred = new Deferred();
    request.post("checkPwd.php", {
      handleAs:"json",
      data: {'user': registry.byId("username").value,
                'pwd': password}

    }).then (function(data){
      if (data.status == "true") {
        console.log ("true");
      }
      else {
        console.log ("false");
      }
    });
   return deferred.promise;
  });
}

For full disclosure, this is what I have now, and it doesn't work.  I'm still unclear on when things happen during the async process.

Thanks,
Harry

From: "Karl Tiedt" <[hidden email]>
To: [hidden email]
Sent: Friday, February 27, 2015 1:44:13 PM
Subject: Re: [Dojo-interest] pwCheck function in PasswordValidator

Your function appears to be fine... your logic problem is the console.log() directly after it that Jared highlighted... Comments added for clarification

    checkPassword(password, successCallback, errorCallback);  // Could take 30 seconds to resolve
    console.log(bPwdValid);  // Called immediately after checkPassword is called (NOT resolved)

Move your console.log to your success and errorCallback or have checkPassword return a deferred that you properly resolve or reject and then you can do something like:

checkPassword().then(.....);

-Karl Tiedt


--
Dojo Toolkit: http://dojotoolkit.org/
Tutorials: http://dojotoolkit.org/documentation/

[hidden email]
To unsubscribe, visit: http://mail.dojotoolkit.org/mailman/listinfo/dojo-interest



--
Dojo Toolkit: http://dojotoolkit.org/
Tutorials: http://dojotoolkit.org/documentation/

[hidden email]
To unsubscribe, visit: http://mail.dojotoolkit.org/mailman/listinfo/dojo-interest

--
Dojo Toolkit: http://dojotoolkit.org/
Tutorials: http://dojotoolkit.org/documentation/

[hidden email]
To unsubscribe, visit: http://mail.dojotoolkit.org/mailman/listinfo/dojo-interest


--
Dojo Toolkit: http://dojotoolkit.org/
Tutorials: http://dojotoolkit.org/documentation/

[hidden email]
To unsubscribe, visit: http://mail.dojotoolkit.org/mailman/listinfo/dojo-interest
Reply | Threaded
Open this post in threaded view
|

Re: pwCheck function in PasswordValidator

Karl Tiedt
Inline, because the wording on this response leads to so many possible misconceptions... And lets be honest, ambiguity is no help to comprehension. Harry, you would benefit greatly from looking over this link regarding Deferreds, http://dojotoolkit.org/documentation/tutorials/1.6/deferreds/

-Karl Tiedt

On Fri, Feb 27, 2015 at 12:16 PM, Stephen Gevers <[hidden email]> wrote:
Harry…You’re going to have to really think through what’s happening at each line.  Notice that where you are returning the deferred is inside the require call back function.  The checkPassword function has no return value.

Harry, this is the key problem with doing inline requires the way you were writing yours.
 
 Any return from the require call back function is ignored.

This is a very broad statement, if returns from require callbacks were ignored, AMD would never work... In this particular case, yes the return is ignored because we never exploit it.
 
 That’s the basis of what your problem is below.  But you seem to be missing the async/deferred/promise concept all together.  And require fits right into that async concept.  When you call checkPassword, the callback function to required will not necessarily be called before the call to checkPassword returns.

That is also an overly generalized statement, the require() callback *HAS* to complete before the checkPassword call "returns" assuming that "returns" means resolves... If by "returns" you mean "go to the next line of code to execute" then you are correct, the entirety of the checkPassword function will complete after that.
 
 Assuming you need your function to be globally defined, you would rework your code to something like this:

Lets not assume this, because we would much rather teach GOOD practices.
 
require ([‘dojo/request’, ‘dojo/Deferred’], function (request, Deferred) {
window.checkPassword = function (password) {
// checkPassword body here…now you can return a Deferred, which will be the result of calling the function, like you have below
};
});

Note that you can’t assume that the checkPassword function is available yet!  If the next line of code after this require was to call checkPassword, it would probably fail.

Aside from not promoting bad habits I have to say, HUH? You just defined checkPassword, it is absolutely safe to assume it is define... Maybe Harry isn't the only one who could use some practice in async behavior ;)
 
 For this reason, it would be better to define checkPassword as its own AMD module.

Yup, you should definitely spend some time reviewing how this works as well... Breaking a single function out into it's own module does nothing to help improve your knowledge of "when its available", and unless this single function is worthy of its own module, you are just creating additional overhead.
 
Here is a fully commented example of how I might right the code with some assumption that this is the executed JS file, not part of a modular piece of code (pastebin here: http://pastebin.com/1KiVHSuv )

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()");
});

 

--
Dojo Toolkit: http://dojotoolkit.org/
Tutorials: http://dojotoolkit.org/documentation/

[hidden email]
To unsubscribe, visit: http://mail.dojotoolkit.org/mailman/listinfo/dojo-interest
Reply | Threaded
Open this post in threaded view
|

Re: pwCheck function in PasswordValidator

Harry Devine
Thank you Karl, Stephen, & Jared.  I won't get back to this until Monday, but I will definitely read up on Monday and get a better handle on it.  It definitely seems like a strategy that will help my overall understanding of Dojo, as well as give me sine ways I can improve some existing projects.

Thanks,
Harry
Sent by Outlook for Android



On Fri, Feb 27, 2015 at 4:51 PM -0800, "Karl Tiedt" <[hidden email]> wrote:

Inline, because the wording on this response leads to so many possible misconceptions... And lets be honest, ambiguity is no help to comprehension. Harry, you would benefit greatly from looking over this link regarding Deferreds, http://dojotoolkit.org/documentation/tutorials/1.6/deferreds/

-Karl Tiedt

On Fri, Feb 27, 2015 at 12:16 PM, Stephen Gevers <[hidden email]> wrote:
Harry…You’re going to have to really think through what’s happening at each line.  Notice that where you are returning the deferred is inside the require call back function.  The checkPassword function has no return value.

Harry, this is the key problem with doing inline requires the way you were writing yours.
 
 Any return from the require call back function is ignored.

This is a very broad statement, if returns from require callbacks were ignored, AMD would never work... In this particular case, yes the return is ignored because we never exploit it.
 
 That’s the basis of what your problem is below.  But you seem to be missing the async/deferred/promise concept all together.  And require fits right into that async concept.  When you call checkPassword, the callback function to required will not necessarily be called before the call to checkPassword returns.

That is also an overly generalized statement, the require() callback *HAS* to complete before the checkPassword call "returns" assuming that "returns" means resolves... If by "returns" you mean "go to the next line of code to execute" then you are correct, the entirety of the checkPassword function will complete after that.
 
 Assuming you need your function to be globally defined, you would rework your code to something like this:

Lets not assume this, because we would much rather teach GOOD practices.
 
require ([‘dojo/request’, ‘dojo/Deferred’], function (request, Deferred) {
window.checkPassword = function (password) {
// checkPassword body here…now you can return a Deferred, which will be the result of calling the function, like you have below
};
});

Note that you can’t assume that the checkPassword function is available yet!  If the next line of code after this require was to call checkPassword, it would probably fail.

Aside from not promoting bad habits I have to say, HUH? You just defined checkPassword, it is absolutely safe to assume it is define... Maybe Harry isn't the only one who could use some practice in async behavior ;)
 
 For this reason, it would be better to define checkPassword as its own AMD module.

Yup, you should definitely spend some time reviewing how this works as well... Breaking a single function out into it's own module does nothing to help improve your knowledge of "when its available", and unless this single function is worthy of its own module, you are just creating additional overhead.
 
Here is a fully commented example of how I might right the code with some assumption that this is the executed JS file, not part of a modular piece of code (pastebin here: http://pastebin.com/1KiVHSuv )

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()");
});

 

--
Dojo Toolkit: http://dojotoolkit.org/
Tutorials: http://dojotoolkit.org/documentation/

[hidden email]
To unsubscribe, visit: http://mail.dojotoolkit.org/mailman/listinfo/dojo-interest
Reply | Threaded
Open this post in threaded view
|

Re: pwCheck function in PasswordValidator

sg3235
In reply to this post by Karl Tiedt

On Feb 27, 2015, at 6:50 PM, Karl Tiedt <[hidden email]> wrote:

Inline, because the wording on this response leads to so many possible misconceptions... And lets be honest, ambiguity is no help to comprehension. Harry, you would benefit greatly from looking over this link regarding Deferreds, http://dojotoolkit.org/documentation/tutorials/1.6/deferreds/

-Karl Tiedt

On Fri, Feb 27, 2015 at 12:16 PM, Stephen Gevers <[hidden email]> wrote:
Harry…You’re going to have to really think through what’s happening at each line.  Notice that where you are returning the deferred is inside the require call back function.  The checkPassword function has no return value.

Harry, this is the key problem with doing inline requires the way you were writing yours.
 
 Any return from the require call back function is ignored.

This is a very broad statement, if returns from require callbacks were ignored, AMD would never work... In this particular case, yes the return is ignored because we never exploit it.

In what cases are returns from require not ignored.  I was under the impression that it was the return from define that made AMD work.  As far as a broad statement.  I’m trying not to be overly detailed because understanding how everything works may just be confusing.
 
 That’s the basis of what your problem is below.  But you seem to be missing the async/deferred/promise concept all together.  And require fits right into that async concept.  When you call checkPassword, the callback function to required will not necessarily be called before the call to checkPassword returns.

That is also an overly generalized statement, the require() callback *HAS* to complete before the checkPassword call "returns" assuming that "returns" means resolves... If by "returns" you mean "go to the next line of code to execute" then you are correct, the entirety of the checkPassword function will complete after that.

I don’t think so.  The require function itself must complete, but the call back is invoked asynchronously.  
 
 Assuming you need your function to be globally defined, you would rework your code to something like this:

Lets not assume this, because we would much rather teach GOOD practices.

I wasn’t assuming it, he did.  I’m just trying to work with what he has.  Later on, I suggest a better method.
 
require ([‘dojo/request’, ‘dojo/Deferred’], function (request, Deferred) {
window.checkPassword = function (password) {
// checkPassword body here…now you can return a Deferred, which will be the result of calling the function, like you have below
};
});

Note that you can’t assume that the checkPassword function is available yet!  If the next line of code after this require was to call checkPassword, it would probably fail.

Aside from not promoting bad habits I have to say, HUH? You just defined checkPassword, it is absolutely safe to assume it is define... Maybe Harry isn't the only one who could use some practice in async behavior ;)

Again…we’re talking async here…if the dependencies aren’t yet loaded, then the call back cannot have been invoked.  Maybe it isn’t me that needs the practice ;)

 
 For this reason, it would be better to define checkPassword as its own AMD module.

Yup, you should definitely spend some time reviewing how this works as well... Breaking a single function out into it's own module does nothing to help improve your knowledge of "when its available", and unless this single function is worthy of its own module, you are just creating additional overhead.

Again, I’m trying to deal with a small problem with very little code to base it on.  You really have no idea how much I understand.  I’m trying to simplify the problem based on what’s going on.  Your bashing me doesn’t really help.  I suggested that the function be its own module so that it wasn’t global and would be available outside of the require in which it is defined.  Without knowing the whole problem, you can’t know if this is needed or not.   We’re both just guessing.  
 
Here is a fully commented example of how I might right the code with some assumption that this is the executed JS file, not part of a modular piece of code (pastebin here: http://pastebin.com/1KiVHSuv )

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()");
});

 
--
Dojo Toolkit: http://dojotoolkit.org/
Tutorials: http://dojotoolkit.org/documentation/

[hidden email]
To unsubscribe, visit: http://mail.dojotoolkit.org/mailman/listinfo/dojo-interest


--
Dojo Toolkit: http://dojotoolkit.org/
Tutorials: http://dojotoolkit.org/documentation/

[hidden email]
To unsubscribe, visit: http://mail.dojotoolkit.org/mailman/listinfo/dojo-interest
Reply | Threaded
Open this post in threaded view
|

Re: pwCheck function in PasswordValidator

Harry Devine
In reply to this post by Karl Tiedt
OK so I'm trying to understand what I've read and the examples given.  I have restructured my main js file to be similar to below:

require([
  "dijit/registry",
  "dojo/dom",
  "dojo/Deferred",
], function (registry, dom, Deferred){

  function validateUserEntered() {
    // check user entered here
  }
  
  // other functions here!
});

The problem I'm running into is I get an error stating that validateUserEntered is not defined.  This was the reason why I had the requires in each subfunction because doing it this was, as was suggested, wouldn't work and would give me these type of errors.  So how do I go about making this work?  I'm moving towards using Deferred on my async functions as was suggested, but in order to get to that function in my execution chain, I have to get this working first.

Thanks!
Harry


From: "Karl Tiedt" <[hidden email]>
To: [hidden email]
Sent: Friday, February 27, 2015 7:50:55 PM
Subject: Re: [Dojo-interest] pwCheck function in PasswordValidator

Inline, because the wording on this response leads to so many possible misconceptions... And lets be honest, ambiguity is no help to comprehension. Harry, you would benefit greatly from looking over this link regarding Deferreds, http://dojotoolkit.org/documentation/tutorials/1.6/deferreds/

-Karl Tiedt

On Fri, Feb 27, 2015 at 12:16 PM, Stephen Gevers <[hidden email]> wrote:
Harry…You’re going to have to really think through what’s happening at each line.  Notice that where you are returning the deferred is inside the require call back function.  The checkPassword function has no return value.

Harry, this is the key problem with doing inline requires the way you were writing yours.
 
 Any return from the require call back function is ignored.

This is a very broad statement, if returns from require callbacks were ignored, AMD would never work... In this particular case, yes the return is ignored because we never exploit it.
 
 That’s the basis of what your problem is below.  But you seem to be missing the async/deferred/promise concept all together.  And require fits right into that async concept.  When you call checkPassword, the callback function to required will not necessarily be called before the call to checkPassword returns.

That is also an overly generalized statement, the require() callback *HAS* to complete before the checkPassword call "returns" assuming that "returns" means resolves... If by "returns" you mean "go to the next line of code to execute" then you are correct, the entirety of the checkPassword function will complete after that.
 
 Assuming you need your function to be globally defined, you would rework your code to something like this:

Lets not assume this, because we would much rather teach GOOD practices.
 
require ([‘dojo/request’, ‘dojo/Deferred’], function (request, Deferred) {
window.checkPassword = function (password) {
// checkPassword body here…now you can return a Deferred, which will be the result of calling the function, like you have below
};
});

Note that you can’t assume that the checkPassword function is available yet!  If the next line of code after this require was to call checkPassword, it would probably fail.

Aside from not promoting bad habits I have to say, HUH? You just defined checkPassword, it is absolutely safe to assume it is define... Maybe Harry isn't the only one who could use some practice in async behavior ;)
 
 For this reason, it would be better to define checkPassword as its own AMD module.

Yup, you should definitely spend some time reviewing how this works as well... Breaking a single function out into it's own module does nothing to help improve your knowledge of "when its available", and unless this single function is worthy of its own module, you are just creating additional overhead.
 
Here is a fully commented example of how I might right the code with some assumption that this is the executed JS file, not part of a modular piece of code (pastebin here: http://pastebin.com/1KiVHSuv )

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()");
});

 

--
Dojo Toolkit: http://dojotoolkit.org/
Tutorials: http://dojotoolkit.org/documentation/

[hidden email]
To unsubscribe, visit: http://mail.dojotoolkit.org/mailman/listinfo/dojo-interest


--
Dojo Toolkit: http://dojotoolkit.org/
Tutorials: http://dojotoolkit.org/documentation/

[hidden email]
To unsubscribe, visit: http://mail.dojotoolkit.org/mailman/listinfo/dojo-interest
Reply | Threaded
Open this post in threaded view
|

Re: pwCheck function in PasswordValidator

jcnorris00
You can do this:


var validateUserEntered = null;

require([
   "dijit/registry",
   "dojo/dom",
   "dojo/Deferred",
], function(registry, dom, Deferred){

   validateUserEntered = function() {
     // check user entered here
   }

   // other functions here!
});


That puts validateUserEntered in global scope, and therefore accessible
from anywhere.  Of course, if you try to call it before it's set, you'll
have other problems.


On 3/2/15 10:54 AM, Harry Devine wrote:

> OK so I'm trying to understand what I've read and the examples given.
>  I have restructured my main js file to be similar to below:
>
> require([
>   "dijit/registry",
>   "dojo/dom",
>   "dojo/Deferred",
> ], function (registry, dom, Deferred){
>
>   function validateUserEntered() {
>     // check user entered here
>   }
>   // other functions here!
> });
>
> The problem I'm running into is I get an error stating that
> validateUserEntered is not defined.  This was the reason why I had the
> requires in each subfunction because doing it this was, as was
> suggested, wouldn't work and would give me these type of errors.  So
> how do I go about making this work?  I'm moving towards using Deferred
> on my async functions as was suggested, but in order to get to that
> function in my execution chain, I have to get this working first.
>
> Thanks!
> Harry
>
> ------------------------------------------------------------------------
> *From: *"Karl Tiedt" <[hidden email]>
> *To: *[hidden email]
> *Sent: *Friday, February 27, 2015 7:50:55 PM
> *Subject: *Re: [Dojo-interest] pwCheck function in PasswordValidator
>
> Inline, because the wording on this response leads to so many possible
> misconceptions... And lets be honest, ambiguity is no help to
> comprehension. Harry, you would benefit greatly from looking over this
> link regarding Deferreds,
> http://dojotoolkit.org/documentation/tutorials/1.6/deferreds/
>
> -Karl Tiedt
>
> On Fri, Feb 27, 2015 at 12:16 PM, Stephen Gevers
> <[hidden email] <mailto:[hidden email]>> wrote:
>
>     Harry…You’re going to have to really think through what’s
>     happening at each line.  Notice that where you are returning the
>     deferred is inside the require call back function.  The
>     checkPassword function has no return value.
>
>
> Harry, this is the key problem with doing inline requires the way you
> were writing yours.
>
>      Any return from the require call back function is ignored.
>
>
> This is a very broad statement, if returns from require callbacks were
> ignored, AMD would never work... In this particular case, yes the
> return is ignored because we never exploit it.
>
>      That’s the basis of what your problem is below.  But you seem to
>     be missing the async/deferred/promise concept all together.  And
>     require fits right into that async concept.  When you call
>     checkPassword, the callback function to required will not
>     necessarily be called before the call to checkPassword returns.
>
>
> That is also an overly generalized statement, the require() callback
> *HAS* to complete before the checkPassword call "returns" assuming
> that "returns" means resolves... If by "returns" you mean "go to the
> next line of code to execute" then you are correct, the entirety of
> the checkPassword function will complete after that.
>
>      Assuming you need your function to be globally defined, you would
>     rework your code to something like this:
>
>
> Lets not assume this, because we would much rather teach GOOD practices.
>
>     require ([‘dojo/request’, ‘dojo/Deferred’], function (request,
>     Deferred) {
>     window.checkPassword = function (password) {
>     // checkPassword body here…now you can return a Deferred, which
>     will be the result of calling the function, like you have below
>     };
>     });
>
>     Note that you can’t assume that the checkPassword function is
>     available yet!  If the next line of code after this require was to
>     call checkPassword, it would probably fail.
>
>
> Aside from not promoting bad habits I have to say, HUH? You just
> defined checkPassword, it is absolutely safe to assume it is define...
> Maybe Harry isn't the only one who could use some practice in async
> behavior ;)
>
>      For this reason, it would be better to define checkPassword as
>     its own AMD module.
>
>
> Yup, you should definitely spend some time reviewing how this works as
> well... Breaking a single function out into it's own module does
> nothing to help improve your knowledge of "when its available", and
> unless this single function is worthy of its own module, you are just
> creating additional overhead.
> Here is a fully commented example of how I might right the code with
> some assumption that this is the executed JS file, not part of a
> modular piece of code (pastebin here: http://pastebin.com/1KiVHSuv )
>
> 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()");
> });
>
>
> --
> Dojo Toolkit: http://dojotoolkit.org/
> Tutorials: http://dojotoolkit.org/documentation/
>
> [hidden email]
> To unsubscribe, visit:
> http://mail.dojotoolkit.org/mailman/listinfo/dojo-interest
>
>
>

--
Dojo Toolkit: http://dojotoolkit.org/
Tutorials: http://dojotoolkit.org/documentation/

[hidden email]
To unsubscribe, visit: http://mail.dojotoolkit.org/mailman/listinfo/dojo-interest
Reply | Threaded
Open this post in threaded view
|

Re: pwCheck function in PasswordValidator

sg3235
In reply to this post by Harry Devine
validateUserEntered is defined in the scope in which it was declared…that means it’s only defined inside the callback function for require.  Without knowing how you are trying to use the function, it’s difficult to say how to fix it.  The idea behind AMD is to limit the pollution of the global name space.  As Karl pointed out, it may not be the best idea to make the validateUserEntered function global.  However, it is easy to do…simply set window.validateUserEntered equal to your function inside the require callback.  However, there are still timing issues.  window.validateUserEnetered will not be defined until the require callback is executed.  And the callback won’t be executed until all of its dependencies have been loaded.  So it’s possible that the loader could still be trying to load “dijit/registry” when you are wanting to use window.validateUserEntered.  It’s unlikely if you are waiting on user input before attempting to use the function that the loader wouldn’t have completed this task.  But it’s important to understand that some unknown amount of JavaScript code can be executed before the callback for this require is invoked.  

My best guess is that you have some JavaScript attached to a DOM element; for example, an onclick attribute on a button.  In this case, you are probably calling some function that has to be defined in the global namespace and that’s why you have gotten hung up on the nesting order of require / function def.  A better way might be to add some code to your require that uses dojo/on to attach a function to that button.  Since the function you would be attaching to handle the click event would be declared in the same scope as validateUserEntered, you wouldn’t need it to be global. 

Steve

On Mar 2, 2015, at 12:54 PM, Harry Devine <[hidden email]> wrote:

OK so I'm trying to understand what I've read and the examples given.  I have restructured my main js file to be similar to below:

require([
  "dijit/registry",
  "dojo/dom",
  "dojo/Deferred",
], function (registry, dom, Deferred){

  function validateUserEntered() {
    // check user entered here
  }
  
  // other functions here!
});

The problem I'm running into is I get an error stating that validateUserEntered is not defined.  This was the reason why I had the requires in each subfunction because doing it this was, as was suggested, wouldn't work and would give me these type of errors.  So how do I go about making this work?  I'm moving towards using Deferred on my async functions as was suggested, but in order to get to that function in my execution chain, I have to get this working first.

Thanks!
Harry


From: "Karl Tiedt" <[hidden email]>
To: [hidden email]
Sent: Friday, February 27, 2015 7:50:55 PM
Subject: Re: [Dojo-interest] pwCheck function in PasswordValidator

Inline, because the wording on this response leads to so many possible misconceptions... And lets be honest, ambiguity is no help to comprehension. Harry, you would benefit greatly from looking over this link regarding Deferreds, http://dojotoolkit.org/documentation/tutorials/1.6/deferreds/

-Karl Tiedt

On Fri, Feb 27, 2015 at 12:16 PM, Stephen Gevers <[hidden email]> wrote:
Harry…You’re going to have to really think through what’s happening at each line.  Notice that where you are returning the deferred is inside the require call back function.  The checkPassword function has no return value.

Harry, this is the key problem with doing inline requires the way you were writing yours.
 
 Any return from the require call back function is ignored.

This is a very broad statement, if returns from require callbacks were ignored, AMD would never work... In this particular case, yes the return is ignored because we never exploit it.
 
 That’s the basis of what your problem is below.  But you seem to be missing the async/deferred/promise concept all together.  And require fits right into that async concept.  When you call checkPassword, the callback function to required will not necessarily be called before the call to checkPassword returns.

That is also an overly generalized statement, the require() callback *HAS* to complete before the checkPassword call "returns" assuming that "returns" means resolves... If by "returns" you mean "go to the next line of code to execute" then you are correct, the entirety of the checkPassword function will complete after that.
 
 Assuming you need your function to be globally defined, you would rework your code to something like this:

Lets not assume this, because we would much rather teach GOOD practices.
 
require ([‘dojo/request’, ‘dojo/Deferred’], function (request, Deferred) {
window.checkPassword = function (password) {
// checkPassword body here…now you can return a Deferred, which will be the result of calling the function, like you have below
};
});

Note that you can’t assume that the checkPassword function is available yet!  If the next line of code after this require was to call checkPassword, it would probably fail.

Aside from not promoting bad habits I have to say, HUH? You just defined checkPassword, it is absolutely safe to assume it is define... Maybe Harry isn't the only one who could use some practice in async behavior ;)
 
 For this reason, it would be better to define checkPassword as its own AMD module.

Yup, you should definitely spend some time reviewing how this works as well... Breaking a single function out into it's own module does nothing to help improve your knowledge of "when its available", and unless this single function is worthy of its own module, you are just creating additional overhead.
 
Here is a fully commented example of how I might right the code with some assumption that this is the executed JS file, not part of a modular piece of code (pastebin here: http://pastebin.com/1KiVHSuv )

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()");
});

 

--
Dojo Toolkit: http://dojotoolkit.org/
Tutorials: http://dojotoolkit.org/documentation/

[hidden email]
To unsubscribe, visit: http://mail.dojotoolkit.org/mailman/listinfo/dojo-interest

--
Dojo Toolkit: http://dojotoolkit.org/
Tutorials: http://dojotoolkit.org/documentation/

[hidden email]
To unsubscribe, visit: http://mail.dojotoolkit.org/mailman/listinfo/dojo-interest


--
Dojo Toolkit: http://dojotoolkit.org/
Tutorials: http://dojotoolkit.org/documentation/

[hidden email]
To unsubscribe, visit: http://mail.dojotoolkit.org/mailman/listinfo/dojo-interest
Reply | Threaded
Open this post in threaded view
|

Re: pwCheck function in PasswordValidator

Harry Devine
Well, I'm defining my button on my page as follows (the main js file has already been included at this point):

<button data-dojo-type="dijit/form/Button" type="button"> Show Account Info
  <script type="dojo/on" data-dojo-event="click" data-dojo-args="evt">
    validateUserEntered();
  </script>
</button>

So I believe I'm already using dojo/on, so why wouldn't the function be found in my js file?

Thanks,
Harry

From: "Stephen Gevers" <[hidden email]>
To: [hidden email]
Sent: Monday, March 2, 2015 2:25:58 PM
Subject: Re: [Dojo-interest] pwCheck function in PasswordValidator

validateUserEntered is defined in the scope in which it was declared…that means it’s only defined inside the callback function for require.  Without knowing how you are trying to use the function, it’s difficult to say how to fix it.  The idea behind AMD is to limit the pollution of the global name space.  As Karl pointed out, it may not be the best idea to make the validateUserEntered function global.  However, it is easy to do…simply set window.validateUserEntered equal to your function inside the require callback.  However, there are still timing issues.  window.validateUserEnetered will not be defined until the require callback is executed.  And the callback won’t be executed until all of its dependencies have been loaded.  So it’s possible that the loader could still be trying to load “dijit/registry” when you are wanting to use window.validateUserEntered.  It’s unlikely if you are waiting on user input before attempting to use the function that the loader wouldn’t have completed this task.  But it’s important to understand that some unknown amount of JavaScript code can be executed before the callback for this require is invoked.  

My best guess is that you have some JavaScript attached to a DOM element; for example, an onclick attribute on a button.  In this case, you are probably calling some function that has to be defined in the global namespace and that’s why you have gotten hung up on the nesting order of require / function def.  A better way might be to add some code to your require that uses dojo/on to attach a function to that button.  Since the function you would be attaching to handle the click event would be declared in the same scope as validateUserEntered, you wouldn’t need it to be global. 

Steve

On Mar 2, 2015, at 12:54 PM, Harry Devine <[hidden email]> wrote:

OK so I'm trying to understand what I've read and the examples given.  I have restructured my main js file to be similar to below:

require([
  "dijit/registry",
  "dojo/dom",
  "dojo/Deferred",
], function (registry, dom, Deferred){

  function validateUserEntered() {
    // check user entered here
  }
  
  // other functions here!
});

The problem I'm running into is I get an error stating that validateUserEntered is not defined.  This was the reason why I had the requires in each subfunction because doing it this was, as was suggested, wouldn't work and would give me these type of errors.  So how do I go about making this work?  I'm moving towards using Deferred on my async functions as was suggested, but in order to get to that function in my execution chain, I have to get this working first.

Thanks!
Harry


From: "Karl Tiedt" <[hidden email]>
To: [hidden email]
Sent: Friday, February 27, 2015 7:50:55 PM
Subject: Re: [Dojo-interest] pwCheck function in PasswordValidator

Inline, because the wording on this response leads to so many possible misconceptions... And lets be honest, ambiguity is no help to comprehension. Harry, you would benefit greatly from looking over this link regarding Deferreds, http://dojotoolkit.org/documentation/tutorials/1.6/deferreds/

-Karl Tiedt

On Fri, Feb 27, 2015 at 12:16 PM, Stephen Gevers <[hidden email]> wrote:
Harry…You’re going to have to really think through what’s happening at each line.  Notice that where you are returning the deferred is inside the require call back function.  The checkPassword function has no return value.

Harry, this is the key problem with doing inline requires the way you were writing yours.
 
 Any return from the require call back function is ignored.

This is a very broad statement, if returns from require callbacks were ignored, AMD would never work... In this particular case, yes the return is ignored because we never exploit it.
 
 That’s the basis of what your problem is below.  But you seem to be missing the async/deferred/promise concept all together.  And require fits right into that async concept.  When you call checkPassword, the callback function to required will not necessarily be called before the call to checkPassword returns.

That is also an overly generalized statement, the require() callback *HAS* to complete before the checkPassword call "returns" assuming that "returns" means resolves... If by "returns" you mean "go to the next line of code to execute" then you are correct, the entirety of the checkPassword function will complete after that.
 
 Assuming you need your function to be globally defined, you would rework your code to something like this:

Lets not assume this, because we would much rather teach GOOD practices.
 
require ([‘dojo/request’, ‘dojo/Deferred’], function (request, Deferred) {
window.checkPassword = function (password) {
// checkPassword body here…now you can return a Deferred, which will be the result of calling the function, like you have below
};
});

Note that you can’t assume that the checkPassword function is available yet!  If the next line of code after this require was to call checkPassword, it would probably fail.

Aside from not promoting bad habits I have to say, HUH? You just defined checkPassword, it is absolutely safe to assume it is define... Maybe Harry isn't the only one who could use some practice in async behavior ;)
 
 For this reason, it would be better to define checkPassword as its own AMD module.

Yup, you should definitely spend some time reviewing how this works as well... Breaking a single function out into it's own module does nothing to help improve your knowledge of "when its available", and unless this single function is worthy of its own module, you are just creating additional overhead.
 
Here is a fully commented example of how I might right the code with some assumption that this is the executed JS file, not part of a modular piece of code (pastebin here: http://pastebin.com/1KiVHSuv )

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()");
});

 

--
Dojo Toolkit: http://dojotoolkit.org/
Tutorials: http://dojotoolkit.org/documentation/

[hidden email]
To unsubscribe, visit: http://mail.dojotoolkit.org/mailman/listinfo/dojo-interest

--
Dojo Toolkit: http://dojotoolkit.org/
Tutorials: http://dojotoolkit.org/documentation/

[hidden email]
To unsubscribe, visit: http://mail.dojotoolkit.org/mailman/listinfo/dojo-interest


--
Dojo Toolkit: http://dojotoolkit.org/
Tutorials: http://dojotoolkit.org/documentation/

[hidden email]
To unsubscribe, visit: http://mail.dojotoolkit.org/mailman/listinfo/dojo-interest


--
Dojo Toolkit: http://dojotoolkit.org/
Tutorials: http://dojotoolkit.org/documentation/

[hidden email]
To unsubscribe, visit: http://mail.dojotoolkit.org/mailman/listinfo/dojo-interest
Reply | Threaded
Open this post in threaded view
|

Re: pwCheck function in PasswordValidator

Harry Devine
In reply to this post by jcnorris00
I just tried your suggestion and it didn't work.  Same error.

Thanks,
Harry


From: "John Norris" <[hidden email]>
To: [hidden email]
Sent: Monday, March 2, 2015 1:58:54 PM
Subject: Re: [Dojo-interest] pwCheck function in PasswordValidator

You can do this:


var validateUserEntered = null;

require([
   "dijit/registry",
   "dojo/dom",
   "dojo/Deferred",
], function(registry, dom, Deferred){

   validateUserEntered = function() {
     // check user entered here
   }

   // other functions here!
});


That puts validateUserEntered in global scope, and therefore accessible
from anywhere.  Of course, if you try to call it before it's set, you'll
have other problems.


On 3/2/15 10:54 AM, Harry Devine wrote:

> OK so I'm trying to understand what I've read and the examples given.
>  I have restructured my main js file to be similar to below:
>
> require([
>   "dijit/registry",
>   "dojo/dom",
>   "dojo/Deferred",
> ], function (registry, dom, Deferred){
>
>   function validateUserEntered() {
>     // check user entered here
>   }
>   // other functions here!
> });
>
> The problem I'm running into is I get an error stating that
> validateUserEntered is not defined.  This was the reason why I had the
> requires in each subfunction because doing it this was, as was
> suggested, wouldn't work and would give me these type of errors.  So
> how do I go about making this work?  I'm moving towards using Deferred
> on my async functions as was suggested, but in order to get to that
> function in my execution chain, I have to get this working first.
>
> Thanks!
> Harry
>
> ------------------------------------------------------------------------
> *From: *"Karl Tiedt" <[hidden email]>
> *To: *[hidden email]
> *Sent: *Friday, February 27, 2015 7:50:55 PM
> *Subject: *Re: [Dojo-interest] pwCheck function in PasswordValidator
>
> Inline, because the wording on this response leads to so many possible
> misconceptions... And lets be honest, ambiguity is no help to
> comprehension. Harry, you would benefit greatly from looking over this
> link regarding Deferreds,
> http://dojotoolkit.org/documentation/tutorials/1.6/deferreds/
>
> -Karl Tiedt
>
> On Fri, Feb 27, 2015 at 12:16 PM, Stephen Gevers
> <[hidden email] <mailto:[hidden email]>> wrote:
>
>     Harry…You’re going to have to really think through what’s
>     happening at each line.  Notice that where you are returning the
>     deferred is inside the require call back function.  The
>     checkPassword function has no return value.
>
>
> Harry, this is the key problem with doing inline requires the way you
> were writing yours.
>
>      Any return from the require call back function is ignored.
>
>
> This is a very broad statement, if returns from require callbacks were
> ignored, AMD would never work... In this particular case, yes the
> return is ignored because we never exploit it.
>
>      That’s the basis of what your problem is below.  But you seem to
>     be missing the async/deferred/promise concept all together.  And
>     require fits right into that async concept.  When you call
>     checkPassword, the callback function to required will not
>     necessarily be called before the call to checkPassword returns.
>
>
> That is also an overly generalized statement, the require() callback
> *HAS* to complete before the checkPassword call "returns" assuming
> that "returns" means resolves... If by "returns" you mean "go to the
> next line of code to execute" then you are correct, the entirety of
> the checkPassword function will complete after that.
>
>      Assuming you need your function to be globally defined, you would
>     rework your code to something like this:
>
>
> Lets not assume this, because we would much rather teach GOOD practices.
>
>     require ([‘dojo/request’, ‘dojo/Deferred’], function (request,
>     Deferred) {
>     window.checkPassword = function (password) {
>     // checkPassword body here…now you can return a Deferred, which
>     will be the result of calling the function, like you have below
>     };
>     });
>
>     Note that you can’t assume that the checkPassword function is
>     available yet!  If the next line of code after this require was to
>     call checkPassword, it would probably fail.
>
>
> Aside from not promoting bad habits I have to say, HUH? You just
> defined checkPassword, it is absolutely safe to assume it is define...
> Maybe Harry isn't the only one who could use some practice in async
> behavior ;)
>
>      For this reason, it would be better to define checkPassword as
>     its own AMD module.
>
>
> Yup, you should definitely spend some time reviewing how this works as
> well... Breaking a single function out into it's own module does
> nothing to help improve your knowledge of "when its available", and
> unless this single function is worthy of its own module, you are just
> creating additional overhead.
> Here is a fully commented example of how I might right the code with
> some assumption that this is the executed JS file, not part of a
> modular piece of code (pastebin here: http://pastebin.com/1KiVHSuv )
>
> 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()");
> });
>
>
> --
> Dojo Toolkit: http://dojotoolkit.org/
> Tutorials: http://dojotoolkit.org/documentation/
>
> [hidden email]
> To unsubscribe, visit:
> http://mail.dojotoolkit.org/mailman/listinfo/dojo-interest
>
>
>

--
Dojo Toolkit: http://dojotoolkit.org/
Tutorials: http://dojotoolkit.org/documentation/

[hidden email]
To unsubscribe, visit: http://mail.dojotoolkit.org/mailman/listinfo/dojo-interest


--
Dojo Toolkit: http://dojotoolkit.org/
Tutorials: http://dojotoolkit.org/documentation/

[hidden email]
To unsubscribe, visit: http://mail.dojotoolkit.org/mailman/listinfo/dojo-interest
Reply | Threaded
Open this post in threaded view
|

Re: pwCheck function in PasswordValidator

fduartej
In reply to this post by sg3235
unsuscribe

2015-03-02 14:25 GMT-05:00 Stephen Gevers <[hidden email]>:
validateUserEntered is defined in the scope in which it was declared…that means it’s only defined inside the callback function for require.  Without knowing how you are trying to use the function, it’s difficult to say how to fix it.  The idea behind AMD is to limit the pollution of the global name space.  As Karl pointed out, it may not be the best idea to make the validateUserEntered function global.  However, it is easy to do…simply set window.validateUserEntered equal to your function inside the require callback.  However, there are still timing issues.  window.validateUserEnetered will not be defined until the require callback is executed.  And the callback won’t be executed until all of its dependencies have been loaded.  So it’s possible that the loader could still be trying to load “dijit/registry” when you are wanting to use window.validateUserEntered.  It’s unlikely if you are waiting on user input before attempting to use the function that the loader wouldn’t have completed this task.  But it’s important to understand that some unknown amount of JavaScript code can be executed before the callback for this require is invoked.  

My best guess is that you have some JavaScript attached to a DOM element; for example, an onclick attribute on a button.  In this case, you are probably calling some function that has to be defined in the global namespace and that’s why you have gotten hung up on the nesting order of require / function def.  A better way might be to add some code to your require that uses dojo/on to attach a function to that button.  Since the function you would be attaching to handle the click event would be declared in the same scope as validateUserEntered, you wouldn’t need it to be global. 

Steve

On Mar 2, 2015, at 12:54 PM, Harry Devine <[hidden email]> wrote:

OK so I'm trying to understand what I've read and the examples given.  I have restructured my main js file to be similar to below:

require([
  "dijit/registry",
  "dojo/dom",
  "dojo/Deferred",
], function (registry, dom, Deferred){

  function validateUserEntered() {
    // check user entered here
  }
  
  // other functions here!
});

The problem I'm running into is I get an error stating that validateUserEntered is not defined.  This was the reason why I had the requires in each subfunction because doing it this was, as was suggested, wouldn't work and would give me these type of errors.  So how do I go about making this work?  I'm moving towards using Deferred on my async functions as was suggested, but in order to get to that function in my execution chain, I have to get this working first.

Thanks!
Harry


From: "Karl Tiedt" <[hidden email]>
To: [hidden email]
Sent: Friday, February 27, 2015 7:50:55 PM
Subject: Re: [Dojo-interest] pwCheck function in PasswordValidator

Inline, because the wording on this response leads to so many possible misconceptions... And lets be honest, ambiguity is no help to comprehension. Harry, you would benefit greatly from looking over this link regarding Deferreds, http://dojotoolkit.org/documentation/tutorials/1.6/deferreds/

-Karl Tiedt

On Fri, Feb 27, 2015 at 12:16 PM, Stephen Gevers <[hidden email]> wrote:
Harry…You’re going to have to really think through what’s happening at each line.  Notice that where you are returning the deferred is inside the require call back function.  The checkPassword function has no return value.

Harry, this is the key problem with doing inline requires the way you were writing yours.
 
 Any return from the require call back function is ignored.

This is a very broad statement, if returns from require callbacks were ignored, AMD would never work... In this particular case, yes the return is ignored because we never exploit it.
 
 That’s the basis of what your problem is below.  But you seem to be missing the async/deferred/promise concept all together.  And require fits right into that async concept.  When you call checkPassword, the callback function to required will not necessarily be called before the call to checkPassword returns.

That is also an overly generalized statement, the require() callback *HAS* to complete before the checkPassword call "returns" assuming that "returns" means resolves... If by "returns" you mean "go to the next line of code to execute" then you are correct, the entirety of the checkPassword function will complete after that.
 
 Assuming you need your function to be globally defined, you would rework your code to something like this:

Lets not assume this, because we would much rather teach GOOD practices.
 
require ([‘dojo/request’, ‘dojo/Deferred’], function (request, Deferred) {
window.checkPassword = function (password) {
// checkPassword body here…now you can return a Deferred, which will be the result of calling the function, like you have below
};
});

Note that you can’t assume that the checkPassword function is available yet!  If the next line of code after this require was to call checkPassword, it would probably fail.

Aside from not promoting bad habits I have to say, HUH? You just defined checkPassword, it is absolutely safe to assume it is define... Maybe Harry isn't the only one who could use some practice in async behavior ;)
 
 For this reason, it would be better to define checkPassword as its own AMD module.

Yup, you should definitely spend some time reviewing how this works as well... Breaking a single function out into it's own module does nothing to help improve your knowledge of "when its available", and unless this single function is worthy of its own module, you are just creating additional overhead.
 
Here is a fully commented example of how I might right the code with some assumption that this is the executed JS file, not part of a modular piece of code (pastebin here: http://pastebin.com/1KiVHSuv )

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()");
});

 

--
Dojo Toolkit: http://dojotoolkit.org/
Tutorials: http://dojotoolkit.org/documentation/

[hidden email]
To unsubscribe, visit: http://mail.dojotoolkit.org/mailman/listinfo/dojo-interest

--
Dojo Toolkit: http://dojotoolkit.org/
Tutorials: http://dojotoolkit.org/documentation/

[hidden email]
To unsubscribe, visit: http://mail.dojotoolkit.org/mailman/listinfo/dojo-interest


--
Dojo Toolkit: http://dojotoolkit.org/
Tutorials: http://dojotoolkit.org/documentation/

[hidden email]
To unsubscribe, visit: http://mail.dojotoolkit.org/mailman/listinfo/dojo-interest




--
Regards,
Fred Duarte

--
Dojo Toolkit: http://dojotoolkit.org/
Tutorials: http://dojotoolkit.org/documentation/

[hidden email]
To unsubscribe, visit: http://mail.dojotoolkit.org/mailman/listinfo/dojo-interest
Reply | Threaded
Open this post in threaded view
|

Re: pwCheck function in PasswordValidator

sg3235
In reply to this post by Harry Devine
You are using dojo/on declaratively.  The function you are calling must be global.  However, if you give your button an id (say acctInfo), you could do this:

require ([‘dojo/dom’, ‘dojo/on’, ‘dijit/registry’, ‘dojo/Deferred’), function, (dom, on, registry, Deferred) {

function validateUserEntered() {
// fn body
};

on(dom.byId(‘acctInfo’), ‘click’, function (evt) {
validateUserEntered().then(function (data) {
// handle the deferred here
});
});
});
On Mar 2, 2015, at 2:48 PM, Harry Devine <[hidden email]> wrote:

Well, I'm defining my button on my page as follows (the main js file has already been included at this point):

<button data-dojo-type="dijit/form/Button" type="button"> Show Account Info
  <script type="dojo/on" data-dojo-event="click" data-dojo-args="evt">
    validateUserEntered();
  </script>
</button>

So I believe I'm already using dojo/on, so why wouldn't the function be found in my js file?

Thanks,
Harry

From: "Stephen Gevers" <[hidden email]>
To: [hidden email]
Sent: Monday, March 2, 2015 2:25:58 PM
Subject: Re: [Dojo-interest] pwCheck function in PasswordValidator

validateUserEntered is defined in the scope in which it was declared…that means it’s only defined inside the callback function for require.  Without knowing how you are trying to use the function, it’s difficult to say how to fix it.  The idea behind AMD is to limit the pollution of the global name space.  As Karl pointed out, it may not be the best idea to make the validateUserEntered function global.  However, it is easy to do…simply set window.validateUserEntered equal to your function inside the require callback.  However, there are still timing issues.  window.validateUserEnetered will not be defined until the require callback is executed.  And the callback won’t be executed until all of its dependencies have been loaded.  So it’s possible that the loader could still be trying to load “dijit/registry” when you are wanting to use window.validateUserEntered.  It’s unlikely if you are waiting on user input before attempting to use the function that the loader wouldn’t have completed this task.  But it’s important to understand that some unknown amount of JavaScript code can be executed before the callback for this require is invoked.  

My best guess is that you have some JavaScript attached to a DOM element; for example, an onclick attribute on a button.  In this case, you are probably calling some function that has to be defined in the global namespace and that’s why you have gotten hung up on the nesting order of require / function def.  A better way might be to add some code to your require that uses dojo/on to attach a function to that button.  Since the function you would be attaching to handle the click event would be declared in the same scope as validateUserEntered, you wouldn’t need it to be global. 

Steve

On Mar 2, 2015, at 12:54 PM, Harry Devine <[hidden email]> wrote:

OK so I'm trying to understand what I've read and the examples given.  I have restructured my main js file to be similar to below:

require([
  "dijit/registry",
  "dojo/dom",
  "dojo/Deferred",
], function (registry, dom, Deferred){

  function validateUserEntered() {
    // check user entered here
  }
  
  // other functions here!
});

The problem I'm running into is I get an error stating that validateUserEntered is not defined.  This was the reason why I had the requires in each subfunction because doing it this was, as was suggested, wouldn't work and would give me these type of errors.  So how do I go about making this work?  I'm moving towards using Deferred on my async functions as was suggested, but in order to get to that function in my execution chain, I have to get this working first.

Thanks!
Harry


From: "Karl Tiedt" <[hidden email]>
To: [hidden email]
Sent: Friday, February 27, 2015 7:50:55 PM
Subject: Re: [Dojo-interest] pwCheck function in PasswordValidator

Inline, because the wording on this response leads to so many possible misconceptions... And lets be honest, ambiguity is no help to comprehension. Harry, you would benefit greatly from looking over this link regarding Deferreds, http://dojotoolkit.org/documentation/tutorials/1.6/deferreds/

-Karl Tiedt

On Fri, Feb 27, 2015 at 12:16 PM, Stephen Gevers <[hidden email]> wrote:
Harry…You’re going to have to really think through what’s happening at each line.  Notice that where you are returning the deferred is inside the require call back function.  The checkPassword function has no return value.

Harry, this is the key problem with doing inline requires the way you were writing yours.
 
 Any return from the require call back function is ignored.

This is a very broad statement, if returns from require callbacks were ignored, AMD would never work... In this particular case, yes the return is ignored because we never exploit it.
 
 That’s the basis of what your problem is below.  But you seem to be missing the async/deferred/promise concept all together.  And require fits right into that async concept.  When you call checkPassword, the callback function to required will not necessarily be called before the call to checkPassword returns.

That is also an overly generalized statement, the require() callback *HAS* to complete before the checkPassword call "returns" assuming that "returns" means resolves... If by "returns" you mean "go to the next line of code to execute" then you are correct, the entirety of the checkPassword function will complete after that.
 
 Assuming you need your function to be globally defined, you would rework your code to something like this:

Lets not assume this, because we would much rather teach GOOD practices.
 
require ([‘dojo/request’, ‘dojo/Deferred’], function (request, Deferred) {
window.checkPassword = function (password) {
// checkPassword body here…now you can return a Deferred, which will be the result of calling the function, like you have below
};
});

Note that you can’t assume that the checkPassword function is available yet!  If the next line of code after this require was to call checkPassword, it would probably fail.

Aside from not promoting bad habits I have to say, HUH? You just defined checkPassword, it is absolutely safe to assume it is define... Maybe Harry isn't the only one who could use some practice in async behavior ;)
 
 For this reason, it would be better to define checkPassword as its own AMD module.

Yup, you should definitely spend some time reviewing how this works as well... Breaking a single function out into it's own module does nothing to help improve your knowledge of "when its available", and unless this single function is worthy of its own module, you are just creating additional overhead.
 
Here is a fully commented example of how I might right the code with some assumption that this is the executed JS file, not part of a modular piece of code (pastebin here: http://pastebin.com/1KiVHSuv )

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()");
});

 

--
Dojo Toolkit: http://dojotoolkit.org/
Tutorials: http://dojotoolkit.org/documentation/

[hidden email]
To unsubscribe, visit: http://mail.dojotoolkit.org/mailman/listinfo/dojo-interest

--
Dojo Toolkit: http://dojotoolkit.org/
Tutorials: http://dojotoolkit.org/documentation/

[hidden email]
To unsubscribe, visit: http://mail.dojotoolkit.org/mailman/listinfo/dojo-interest


--
Dojo Toolkit: http://dojotoolkit.org/
Tutorials: http://dojotoolkit.org/documentation/

[hidden email]
To unsubscribe, visit: http://mail.dojotoolkit.org/mailman/listinfo/dojo-interest

--
Dojo Toolkit: http://dojotoolkit.org/
Tutorials: http://dojotoolkit.org/documentation/

[hidden email]
To unsubscribe, visit: http://mail.dojotoolkit.org/mailman/listinfo/dojo-interest


--
Dojo Toolkit: http://dojotoolkit.org/
Tutorials: http://dojotoolkit.org/documentation/

[hidden email]
To unsubscribe, visit: http://mail.dojotoolkit.org/mailman/listinfo/dojo-interest
12