【问题标题】:How organize asynchronous codes with Dojo?如何用 Dojo 组织异步代码?
【发布时间】:2013-01-19 16:03:33
【问题描述】:

我正在创建一个带有动态 TAB(来自 RESTful 的数据)的 webapp,每个 TAB 都有一个 dgrid,我从 RESTful 中获取列,从 RESTful 中获取行。我用 XHR 和 MemoryStore 让一切都很好,但我现在需要从 XHR 更改为 JsonRest,因为我需要向服务器传递一个 HTTP Range。

我在使用 Dojo 中的异步调用来组织我的代码时遇到了困难。我给你举个例子:

method1() - Sync 
method2() - Async (JsonRest) 
method3() - Sync 

只有在 method2() 准备好之后,才能执行 method3() 的最佳方式是什么?

我找到了一个名为 WHEN 的类。看起来不错。但是您如何在 dojo 中使用异步应用程序?

我现在最大的问题:我不能通过方法来分离我的代码,我需要把我所有的代码放在 JsonRest 的 promise 函数中(THEN)。因为在 THEN 里面我不能访问另一个方法。

【问题讨论】:

    标签: javascript design-patterns asynchronous dojo restful-url


    【解决方案1】:

    我同意使用 Dojo 的 promise 实现的建议。

    如果您不习惯承诺,这可能会帮助您更快地理解它:http://jsfiddle.net/27jyf/9/。另一个不错的功能是错误处理,我鼓励您在完成基本排序后阅读此内容。

    require(["dojo/Deferred", "dojo/when"], function(Deferred, when) {
        var sayHello = function() { return 'hello' };
        var sayWorld = function() {
            var deferred = new Deferred();        
            window.setTimeout(function() {
                deferred.resolve('world');
            }, 1000);        
            return deferred.promise;
        };
        var sayBang = function() { return '!' };
    
        //This will echo 'hello world !'
        //That's probably how you want to sequence your methods here
        var message = [];
        message.push(sayHello());
        sayWorld().then(function(part) {
            message.push(part);
            message.push(sayBang());
            console.debug(message.join(' '));
        });    
    
        //This will also echo 'hello world !'
        //This probably not the syntax that you want here, 
        //but it shows how to sequence promises and what 'when' actually does
        var message2 = [];
        when(sayHello())
        .then(function(part) {
            message2.push(part);
            return sayWorld();
        })
        .then(function(part) {
            message2.push(part);
            return when(sayBang());
        })
        .then(function(part) {
            message2.push(part);
            console.debug(message2.join(' '));
        }); 
    
        //Provided the behavior observed above, this will echo 'hello !'
        //dojo/when allows you to use the same syntax for sync and async... 
        //but it does not let you magically write async operations in a sync syntax
        //'world' will be pushed into the array a second later, after the message has already been echoed
        var message3 = [];
        message3.push(sayHello());
        when(sayWorld(), function(part) {
            message3.push(part);
        });
        message3.push(sayBang());
        console.debug(message3.join(' '));
    });
    

    【讨论】:

    • 请注意,JsonRest 实际上使用了 Promise API 本身。所以你可以使用 var promise = myJsonRest.get(); promise.then(function(){/*我的成功行为*/}, function() {/*我的失败行为*/});
    【解决方案2】:

    您可以使用promise api 按指定顺序运行异步/同步方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-08
      • 2011-04-17
      • 1970-01-01
      • 1970-01-01
      • 2019-09-29
      • 2018-10-12
      • 1970-01-01
      • 2011-04-16
      相关资源
      最近更新 更多