2010-01-04 39 views
0

填充xml元素我已創建javascript對象和相關聯的方法,該方法看起來非常相似,這一點。 「getUserInfo」 在$())執行。就緒(函數({}使用從AJAX

function userObject(userId) 
{ 
    this.userId = userId; 

    this.getUserMachineList = function(infoId, machineListId) 
    { 
     jQuery.post(machDBPHPPath + "/listAllUserMachineAccess.php", 
      { userId : this.userId, sid : Math.random() }, 
      function(xmlDoc) 
      { 
       var userMachineListXml = document.createElement("xml"); 
       //populate serialized xml in dom element here 
      } 
    }); 
} 

我想讀的人口XML元素的含量,在populatePage(),見下文。

的問題是(我敢肯定,你們中許多人之前已經看到這一點),是由「getUserInfo」創建的XML元素,當我打電話populateUserPage,這是基於在該XML元素的信息做進一步的Ajax調用不存在。

$().ready(function() 
{ 
    //create sessionUser here.. 
    sessionUser.getUserMachineList(USER_MACHINE_INFO_ID, USER_MACHINE_XML_ID); 
    populatePage(); 
}); 

我使用的setTimeout與populatePage,作爲PAS解決方法噸,但不喜歡這 - 這是一個黑客,而並不總是有效。

理想的情況下有一些方法,以等待在我不知道的DOM,這將是巨大的,但還沒有找到作爲尚未存在這個ID ..

或者這可能是一個一般的網頁設計缺陷,我應該重新設計我的服務器端代碼考慮到這種異步的煩躁?

感謝您的幫助..

拉里

回答

0

後面保持我的代碼從功能getUserMachineList的Ajax回調的想法是把這個代碼的通用爲其他頁面,用不同的延遲性肌肉痠痛/格式/目的。該解決方案最終被簡單 - 我在getUserMachineList回調使用「EVAL」,並採取getUserMachineList的參數的具體功能我想用它來填充這個頁面級「populatePage」。這裏是代碼:

//from a global js file 
this.getUserMachineList = function(infoId, machineListId, jsCmd, jsArg) 
{ 
    jQuery.post(machDBPHPPath + "/listAllUserMachineAccess.php", 
     { userId : this.userId, sid : Math.random() }, 
     function(xmlDoc) 
     { 
      var userMachineListXml = document.createElement("xml"); 
      //populate serialized xml to element 

      //do page specific stuff here, after my xml element is populated 
      eval(jsCmd + "(\"" + jsArg + "\")"); 
     }); 
} 

//from my page specific js file, loaded after the above code. the function 
//"populatePage" takes this serialized xml created by getUserMachineList and 
//populates my page appropriately 
$().ready(function() 
{ 
    sessionUser = new userObject(getUserIdFromCookie()); 
    sessionUser.getUserMachineList(USER_MACHINE_INFO_ID, USER_MACHINE_XML_ID, 
     "populatePage",""); 
}); 

看來你可以用這種模式將回調函數鏈接在一起。不理想,但喜歡它比黑客的setTimeout更好..

感謝多,這是很好的是在展示 -

拉里