Reputation: 4944
I'm writing a Backbone + RequireJS project, and encountering the following situation:
Module A:
Backbone.Mediator.publish('ajax:fetch:in:module:b');
// I need to do something like **$.ajax(options).done()** here
Module B:
subscriptions: {
'ajax:fetch:in:module:b': fetch
},
fetch: {
$.ajax(options);
}
I've tried to hook $.ajax(options)
under a shared namespace (like cache.temp = $.ajax(options)
) in Module B, then calling cache.temp.done()
inside Module A, but it happened way before $.ajax(options)
was created so cache.temp
was simply an undefined
.
I guess one way to solve this problem is to create a deferred that delays the execution of the code before $.ajax(options)
is ready, but am not very sure whether this is something doable. Or if there are better ideas around, I am all ears.
Upvotes: 1
Views: 889
Reputation: 406
Here is an simple example of requirejs working with jquery deferred:
moduleA.js
define(['require', 'jquery'], function(require, $) {
var deferred = $.Deferred();
require(['moduleB'], function(moduleB) {
deferred.resolve(moduleB);
});
return deferred.promise();
});
app.js
require(['moduleA'], function(deferred) {
deferred.done(function(moduleA) {
console.log(moduleA);
});
});
Upvotes: 4
Reputation: 5088
You could use $.when
and $.then
like this if you have a ajax or deferred obj.
$.when( ajax call).then( do other thing)
Upvotes: 0