I recently refactored my javascript/jquery application code to use the module pattern.
I have two modules, lets say A and B.
Module B contains a public method(say, Bmethod) that makes a jQuery AJAX JSONP call($.ajax) and passes the response in a callback.
Inside Module A there is a call to B.Bmethod() with a callback function to handle the returned response.
Here is module B's definition:
var B = (function()
{
var obj = {};
obj.Bmethod = function(data, callback)
{
//do JSONP AJAX call
callback(response);
}
return obj;
}());
Now, here's module A's definition with the method call on module B
var A = (function()
{
var doAjax = function(data)
{
B.Bmethod(data, function(response)
{
//Do something with the response
});
}
}());
Here's how I load the module A and start the code execution:
$(document).ready(function()
{
A.doAjax(data);
});
The problem here is that, I have different behavior on Chrome and Firefox. On Chrome, the AJAX call is not being executed at all. There is no request sent. However, on Firefox, I can see the request made and also get a response back, but I do not receive a success callback.
If I put all of this code outside of modules, in just one single file, everything seems to work properly.
I have seen a lot of people(on StackOverflow as well) using the module pattern with AJAX calls successfully, but have not been able to figure out what I am doing wrong.
Any ideas/solutions?