How to send this variable "dt" to the promise.done?

0

I have the following line of javascript:

...
var refundVal = $( ".refundVal" ).val();
var dt={ 
         orderId:orderId,
         refundVal:refundVal,
         process:btnProcess
        };

window.srObjc.confirm("message $"+refundVal+" ?").promise.done(function(dt) 
{
         console.log("test0");  //L1
         console.log(dt);       //L1
         console.log("test1");  //L1
         console.log(refundVal);//L4

         //Ajax
         var request =$.ajax({
                               url: "pagamentos.php",
                               type: "POST",
                               data: dt,
                               dataType: "json"
                              });

         request.done(function(dataset){  

                                  var resut = dataset;
                                  var str = JSON.stringify(resut, null, 2);
                                  alert(str);//test


                                  container.html(render(resut));
                                  $('#raw-data').html(syntaxHighlight(str));
                                  console.log(str);//test
                         }); 

}//window.srObjc.confirm

In this example the value refundVal is equal to $ 1.

This is the result in console.log:

Thevalueof"dt" is showing "ok". I expected to see an object with values in the doconsole log. And the value of refund val is correct = $ 1, and I was not expecting this since I'm not sending this value in the parameter of the promise.done(function(dt) function.

So the question is how do I send this object into the function and see the correct display in console.log?

    
asked by anonymous 15.08.2017 / 22:09

1 answer

0

What happens is that by adding dt to the promise callback function, here in this line:

window.srObjc.confirm("message $"+refundVal+" ?").promise.done(function(dt)

You end up creating a new variable in the scope of the function, but with the same name as the variable dt that is in global scope, in this part of the code:

var dt={ 
     orderId:orderId,
     refundVal:refundVal,
     process:btnProcess
    };

If you remove the variable from the callback function, or change its name, you have the desired result.

To better understand scope I suggest this article that explains as well as the scope of variables.

    
16.08.2017 / 13:51