3

したがって、ここに、 CompoundJSフレームワークに基づくアプリケーションとそのコントローラーがあります。

load('application');

action('index', function () {
    someMethodWithAsyncCallback({}, function () {
        /* async preparing some params for template */
    });

    anotherMethodWithAsyncCallback({}, function () {
        /* async preparing another params for template */
    });

    // another couple of async calls

    // rendering index template
    render('index', { /* using async params */ });
});

index問題は、すべてのコールバックの終了後にテンプレートをレンダリングする方法です。

たぶん、この回答$.whenで説明されているjQueryのようなものがありますか?

4

1 に答える 1

2

概念的には、待機している非同期呼び出しの数を追跡する必要があります。これについては、以下のコードで説明します。しかし、これをより一般的でエレガントにし、より多くの制御を可能にする小さなライブラリがたくさんあります(それらが順番に実行されるようにするなど)。たとえば、 async.js


load('application');

action('index', function () {

    var asyncCounter = 2; // Or whatever number of async you want to complete...
    var completeFunc = function() {
        if (--asyncCounter > 0) {
            return;
        }
        // rendering index template
        render('index', { /* using async params */ });
    };

    someMethodWithAsyncCallback({}, function () {
        /* async preparing some params for template */
        completeFunc();
    });

    anotherMethodWithAsyncCallback({}, function () {
        /* async preparing another params for template */
        completeFunc();
    });

    // another couple of async calls

});
于 2013-01-27T14:06:55.387 に答える