I have function bar which I need to call. I’m new to using callbacks, from what I understand the callback is still inside the scope of the ajax so it can’t see bar. Is it possible to call bar on ajax success? bar is defined in module top.
define(["top"], function() {
function foo(callback) {
$.ajax({
type: "GET",
cache: false,
dataType: 'json',
url: "http://asdf/qwer",
success: function(response) {
callback(response);
}
});
}
foo(function(response) {
bar(response);
});
});
Assuming your
top.jslooks similar to this:(Note that the function has to be exported/returned here!)
You can access the
bar()function like this:See the require.js doc on working with dependencies inside a
define()function.You should add a parameter to the function inside
define()for every required module. In the example case this is thetopparameter. Afterwards you can call all exported properties of the requirement module by using this parameter.