1

取り組んでいるプロジェクトで theJIT.org の spacetree プラグインを使用していますが、ユーザーが求める新機能に問題があります。

従業員のデータベースにクエリを実行し、レポートする人がいるかどうかを確認する小さなスクリプトをまとめました。レポートがある場合は 1 を出力し、そうでない場合は 0 を出力します。現在は次のようになっています (少し面倒ですが、すべてをクリーンアップする前に動作させようとしています):

// Returns either 0 or 1 for if a person has direct reports, used in hierarchy below
function checkManager(empid){
    return $.ajax({
        url: path + 'CorpDir_CheckManager.cfm?Empid='+empid,
        datatype: 'text',
        success: (function (mng) {
            console.log(mng + " " + empid); //This works, but ends up being after the string is built
            return mng;
        }),
        error: (function (){
            alert("ERROR!");
        })
    });
}

//Setting up the JSON for the hierarchy tree. 
function startTree() {

    // Clear all other trees on the page
    for (var i = 0; i < 50; i++) {
        $('#hier' + i + '-canvaswidget').remove();
    }
    var json = '';
    jQuery.getJSON(path + 'CorpDir_ReportingChain.cfm?Empid=' + empid, function (data) {// JSON from reporting chain is data
        jQuery.getJSON(path + 'CorpDir_Subordinate.cfm?Empid=' + empid, function (data2) {  // JSON from subordinate is data2
            // Check to see if there is even a reporting chain
            if(data.DATA.length > 0){
                // This is pretty much magic, it took me a while to work it all out. 
                // This first for loop goes through the information from Reporting Chain, turning into the json that the hierarchy uses
                for (var i = data.DATA.length - 1; i >= 0; i--) {
                    json = json + 'id: "' + data.DATA[i][3].replace(/\s/g, '') + '",name: "' + data.DATA[i][0].replace(/\s/g, '') + '<br>' + data.DATA[i][1].replace(/\s/g, '') + '",data: {xid: "' + data.DATA[i][6].replace(/\s/g, '') + '", "parentnode": "1"},children: [{';
                }
            }
            // This appends the current person to the hierarchy (and will later be set to the root node for it)
            var mng = checkManager(empid); //results in [object Object] instead of 1 or 0
            json = json + 'id: "' + empid + '",name: "' + fname + '<br>' + lname + '",data: {xid: "' + commitid.replace(/\s/g, '') + '", "parentnode":"' + mng + '"},children: [';
            // These are the child nodes (from subordinate JSON date), set up to what the hierarchy wants for it.
            for (var i = 0; i < data2.DATA.length; i++) {
                if ( data2.DATA[i][4].replace(/\s/g, '') != empid ){
                    mng = checkManager(data2.DATA[i][4].replace(/\s/g, ''));
                    json = json + '{id: "' + data2.DATA[i][4].replace(/\s/g, '') + '",name: "' + data2.DATA[i][0] + '<br>' + data2.DATA[i][1] + '",data: {xid: "' + data2.DATA[i][6].replace(/\s/g, '') + '", "parentnode":"' + mng + '"},children: []},';
                }
                // IE doesn't always play well if there's a comma at the end of a list, so this cuts it off if it's there
                if (i === data2.DATA.length - 1) {
                    json = json.slice(0, -1);
                }
            }
            json = json + ']';
            // This finishes up the JSON string, closing off all the brackets left open from setting up the reporting chain
            for (var i = data.DATA.length; i > 0; i--) {
                json = json + '}]';
            }
            json = '{' + json + '}';
            console.log(json);
            // Give all the information we need to the spacetree so we can load up that hierarchy
            jitSpaceTree(json, index, empid);
        });
    });
}

明らかに、その ajax 呼び出しで返されるのを待つと仮定するのは悪い考えでした (とにかくその関数を呼び出した後に移動するのはいいことです:/)、そこにこの情報を取得する最良の方法は何ですか? レポートと従属チェーンを取得する呼び出しを変更することは、現在のユーザー (途中で追加されたユーザー) を確認する必要があるため、部分的な解決策にすぎません。

これを行う正しい方法は何ですか?for ループで非同期呼び出しを行うか、大量のコードを書き直す必要があります。これが現在私が目にしている唯一の方法です。

4

0 に答える 0