繁体   English   中英

Backbone + RequireJS - 延迟等待创建另一个延迟对象

[英]Backbone + RequireJS - A Deferred that awaits the creation of another deferred object

我正在写一个Backbone + RequireJS项目,遇到以下情况:

模块A:

Backbone.Mediator.publish('ajax:fetch:in:module:b');

// I need to do something like **$.ajax(options).done()** here

模块B:

subscriptions: {
    'ajax:fetch:in:module:b': fetch
},

fetch: {
    $.ajax(options);
}

我试图在模块B中的共享命名空间(如cache.temp = $.ajax(options) $.ajax(options)cache.temp = $.ajax(options) ,然后在模块A中调用cache.temp.done() ,但它发生了在$.ajax(options)创建之前,所以cache.temp只是一个undefined

我想解决这个问题的一种方法是创建一个延迟,在$.ajax(options)准备就绪之前延迟代码的执行,但我不太确定这是否可行。 或者如果周围有更好的想法,我会全力以赴。

这是一个使用jquery延迟的requirejs的简单示例:

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);
    });
});

如果你有ajax或deferred obj $.then你可以使用$.when$.then

$.when( ajax call).then( do other thing)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM