Strange behavior with ajax call complete in JavaScript modules
- by user2598794
I have 3 simple modules with JavaScript code and JQuery ajax call. First module lots.js:
var Lots = (function ($) {
var self = this;
var processIsRunning;
return {
getLots: function (lotsUrl) {
var items = [];
self.processIsRunning = true;
var request = $.ajax({
url: lotsUrl,
type: 'POST',
success: function (data) {
//some code
}
});
$.when(request).done(function() {
//some code
self.processIsRunning = false;
});
},
isComplete: function () {
return !self.processIsRunning;
}
};
}(jQuery));
Module bids.js:
var Bids = (function ($) {
return {
makeBids: function (bidUrl) {
//some code
}
};
}(jQuery));
Module app.js which bundles all together:
var App = (function () {
var lots_url = null;
var bid_url = null;
var self = this;
return {
if (!self.lots_url) {
self.lots_url = lotsUrl;
}
GetLots: function (lotsUrl) {
Lots.getLots(self.lots_url);
},
MakeBids: function makeBid(bidUrl) {
//some code
var isComp = Lots.isComplete();
while (!isComp) {
isComp = Lots.isComplete();
}
Bids.makeBids(self.bid_url);
}
};
}());
But in the 'while' loop I always get 'isComplete=false'. In debug I see that 'processIsRunning' in Lots module is always true. What's the problem?