0

この質問の仕方が少しわからないので、今いるところからできる限りのことを始めます。私はデータ型を持っています。これを「プログラム」と呼びます。これは、単一のデータソースからいくつかのコア情報をロードします。中央データ モデルの読み込み/キャッシュ ロジックを作成しましたが、プロジェクト設定に基づいて、フレームワークがデータ モデルに追加する必要がある外部から読み込まれたノードがあります。

私が探しているのは、プロジェクトで必要な場合にこれらの追加されたデータ ノードをロードし、すべてのデータがロードされるまでプログラムを実行しないようにする設計パターンです。ノードがどのようなものになるか、必要になったときにどこでデータを取得するかを事前に知っていると想定できます。私の現在の設定は以下のようになります。

var programs = {},
    loadCuePoints = function (uuid, callback) {
        //will call the callback with the data once loaded and add the program
        //to programs, keyed by the uuid
    },
    loadLinkedFiles = function (uuid, callback) {
        //will append the data to programs[uuid] and call the callback with the
        //data once loaded
    },
    loadProgram = function (uuid, callback) {
        //will append the data to programs[uuid] and call the callback with the
        //data once loaded
    },
    // hash where the key is the node on program and the value is the function
    // used to load the data, this will be based on project settings, but I'm not
    // concerned with this logic for this question
    requiredData = {
        cuePoints : loadCuePoints,
        linkedFiles : loadLinkedFiles
    },
    getProgram = function(uuid, callback) {
        if (programs[uuid]) {
            callback(programs[uuid]);
        } else {
            //assume the key in the requiredData hash is the required node on
            //Program, and that the value is the callback method, the functions
            //in this table sre already set up to load the data and return it
            //via the callback once loaded
        }
    }

私は確かにこれをハックすることができるので、解決策をあまり求めていません (非常にうまく機能するものや特にエレガントなものがない限り)。非同期アクションのコレクション。説明が不明確な場合は、詳しく説明していただければ幸いです。

4

1 に答える 1

1

JavaScript を使用した非同期プログラミングの場合は、promise パターンを使用する必要があります。When.js または jquery.deffered を使用して問題を解決できます。jqueryを使った疑似コードを以下に書きます

  function oneArrayJquery(value) {
            var deffered = jQuery.Deferred();
            deffered.resolve(value);
            return deffered;
        }

        function loadAllArrayValues(imagearray) {
            var deffered = [];
            for (var i = 0; i < imagearray.length; i++) {
                deffered.push(oneArrayJquery(imagearray[i]));
            }
            return deffered;
        }

        var arrayvalue = [1, 3, 4];

        jQuery.when(loadAllArrayValues(arrayvalue)[0], loadAllArrayValues(arrayvalue)[1]).then(function (value1,value2) {
            alert(value1);
            }
        )
于 2012-06-21T17:32:54.313 に答える