我一直在将来自 theJIT.org 的 spacetree 插件用于我正在从事的项目,但在用户想要的新功能方面遇到了麻烦。
我编写了一个小脚本来查询数据库中的员工,看看他们是否有向他们报告的人,如果有,则打印 1,否则打印 0。我为 spacetree 插件设置 json 字符串的代码对现在看起来像这样(它有点乱,但我只是想在我把它全部清理干净之前让它工作):
// 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 循环中进行异步调用或重写大量代码是我现在看到的唯一方法。