Passing data to a member functions that use a function

328 views Asked by At

I have a cfc that is a service. It only has functions. Up until now did not have any member variables.

login.cfc

function post(required string email, required string password) { 

  ...

  variables.password = arguments.password; // wish I didn't have to do this
  var User = entityLoad("Users", {email : arguments.email}).filter(
      function(item){
        return item.validatePassword(variables.password);
      });
  variables.password = "";
  ...

I don't like that I have to set arguments.password to variables.password just so that the function inside of .filter can see it. Isn't there a cleaner way to do this?

1

There are 1 answers

3
Redtopia On BEST ANSWER

In CF11 and newer, including Lucee 4/5, CFML closures can access variables in the parent scope (and up the stack). CF10 seems to have problems with this... but here's the code you can run in https://trycf.com to see how it works on each version of ColdFusion:

<cfscript>
function doFilter(term) {
    var superheroes=[
           {"name":"Iron Man","member":"Avengers"},
           {"name":"Wonder Woman","member":"Justice League"},
           {"name":"Hulk","member":"Avengers"},
           {"name":"Thor","member":"Avengers"},
           {"name":"Aquaman","member":"Justice League"}
     ];

    var filtered=superheroes.filter(function(item){
       return item.member==term;
    });
    writeDump(filtered);
}

doFilter("Avengers");
</cfscript>

So, in other words, you should have access to the arguments in the post() method if you're using CF11 or newer, or Lucee.