jquery - Multiple call from same javascript function with ajax, object in parameters not the same between the function body and the ajax success callback -


i've got strange result when called javascript method ajax. when user click on arrow, load data, don't want user stuck let him change when want. , when data come server, want load data @ object in collection.

html ( little more complex hard paste , think it's enough understand) :

  <a href='#' data-bind='click:loadexactresult'>next</a> 

js:

 function loadexactresult(dateobj) {     //method body     (function(dateobj) {         var payload = {             searchcriteriaparam: "",         };          console.log(moment() + " - calling exact date: " + dateobj().date());         dateobj().isallflightloading(true);         $.ajax({             url: 'bla/bla',             data: payload,             cache: false,             success: function(result) {                 console.log(moment() + " - exact date success: " + dateobj().date());                 // ajax body.             }         });     })(dateobj); } 

but data come last loaded object... here console log.

1410377364469 - calling exact date: mon jan 05 2015 22:00:00 gmt-0500 (est)  1410377365234 - calling exact date: sun jan 04 2015 21:10:00 gmt-0500 (est)  1410377365539 - calling exact date: sat jan 03 2015 20:40:00 gmt-0500 (est)  1410377367559 - exact date success: sat jan 03 2015 20:40:00 gmt-0500 (est)  1410377370488 - exact date success: sat jan 03 2015 20:40:00 gmt-0500 (est)  1410377375648 - exact date success: sat jan 03 2015 20:40:00 gmt-0500 (est) 

arrays , objects passed reference in javascript. you're not getting val, you're getting reference dateobj if changed anywhere changed everywhere. need "break" reference modifying parameter being passed self executing function.

function loadexactresult(dateobj) {   //method body   (function(dateobj) {       var payload = {           searchcriteriaparam: "",       };        console.log(moment() + " - calling exact date: " + dateobj().date());       dateobj().isallflightloading(true);       $.ajax({           url: 'bla/bla',           data: payload,           cache: false,           success: function(result) {               console.log(moment() + " - exact date success: " + dateobj().date());               // ajax body.           }       });   })($.extend(true, {}, dateobj)); } 

Comments

Popular posts from this blog

javascript - how to protect a flash video from refresh? -

android - Associate same looper with different threads -

visual studio 2010 - Connect to informix database windows form application -