I have created a javascript object and associated method which looks very similar to this. "getUserInfo" is executed in $().ready(function(){}
我创建了一个javascript对象和相关的方法,看起来非常类似于此。 “getUserInfo”在$()。ready(function(){}中执行
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
}
});
}
I am trying to read the content of the populated xml element, in populatePage(), see below.
我试图在populatePage()中读取填充的xml元素的内容,见下文。
The problem is (I'm sure many of you have seen this before), is that the xml element created by "getUserInfo" does not exist when I call populateUserPage, which is doing further ajax calls based on information in this xml element.
问题是(我相信很多人之前已经看过这个),当我调用populateUserPage时,“getUserInfo”创建的xml元素不存在,而populateUserPage正在根据此xml元素中的信息进行进一步的ajax调用。
$().ready(function()
{
//create sessionUser here..
sessionUser.getUserMachineList(USER_MACHINE_INFO_ID, USER_MACHINE_XML_ID);
populatePage();
});
I've used setTimeout with populatePage, as a workaround in the past, but don't like this- it's a hack, and doesn't always work.
我已经将setTimeout与populatePage一起使用,作为过去的解决方法,但不喜欢这个 - 它是一个黑客,并不总是有效。
Ideally there's some method to wait for this id to exist in the DOM that I don't know about, which would be great, but haven't found as of yet..
理想情况下有一些方法等待这个id存在于我不知道的DOM中,这将是很好的,但还没有找到...
Or this could be a general web-design flaw, and I should redesign my server side code to take into consideration this asynchronous-ness?
或者这可能是一般的网页设计缺陷,我应该重新设计我的服务器端代码以考虑这种异步性?
Thanks for your help..
谢谢你的帮助..
-Larry
-Larry
1 个解决方案
#1
0
The idea behind keeping my code out of the ajax callback of the function getUserMachineList was to keep this code generic for other pages, with different doms/formats/purposes. The solution ended up being simple- I use "eval" in the callback of getUserMachineList, and take as an argument of getUserMachineList the specific function I want to use to populate this page- "populatePage". Here's the code:
将我的代码保留在函数getUserMachineList的ajax回调之后的想法是保持此代码对于具有不同dom /格式/目的的其他页面通用。解决方案最终变得简单 - 我在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","");
});
It seems you can chain together functions in the callback with this pattern. Not ideal, but like it much better than a setTimeout hack..
看来你可以用这种模式将回调中的函数链接在一起。不理想,但它比setTimeout黑客好得多..
thanks much and it's good to be on the show-
非常感谢,很高兴参加展会 -
-Larry
-Larry
#1
0
The idea behind keeping my code out of the ajax callback of the function getUserMachineList was to keep this code generic for other pages, with different doms/formats/purposes. The solution ended up being simple- I use "eval" in the callback of getUserMachineList, and take as an argument of getUserMachineList the specific function I want to use to populate this page- "populatePage". Here's the code:
将我的代码保留在函数getUserMachineList的ajax回调之后的想法是保持此代码对于具有不同dom /格式/目的的其他页面通用。解决方案最终变得简单 - 我在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","");
});
It seems you can chain together functions in the callback with this pattern. Not ideal, but like it much better than a setTimeout hack..
看来你可以用这种模式将回调中的函数链接在一起。不理想,但它比setTimeout黑客好得多..
thanks much and it's good to be on the show-
非常感谢,很高兴参加展会 -
-Larry
-Larry