全局变量的替代品?
我正在尝试思考全局变量的替代方案。
所讨论的情况是我需要在一个 XML 中查找值并与另一个 XML(或更多)进行比较。由于 XML JQuery 本身就是一个函数,并且其下面的操作是函数内部的函数(呃),因此我无法从 2 个函数深度获取值并在全局范围内使用它。
因此,目前我无法从 1 个文件中获取 XML 值并使用它来过滤另一个 XML 文件,这就是我需要帮助的地方。
我收到了 3 个 XML 文件。
文件 1 -categories.xml - 包含类别映射 例如...
<CAT>
<OA1>True</OA1>
<OA2>False</OA2>
<OA3>True</OA3>
<EP1>True</EP1>
<EP2>False</EP2>
<EP3>False</EP3>
</CAT>
文件 2 = oa.xml - 包含每个 OA 记录的值 例如...
<OA>
<Name>Name 1</Name>
<City>City</City>
<State>ST</State>
</OA>
等等...
文件 3 = EP.xml - 包含每个 EP 记录的值 。
<EP>
<object 1></object1>
<object 2></object2>
<object 3></object3>
</EP>
现在,我认为我开始时可以做的是允许用户选择一个类别,并根据该选择返回 2 个包含映射到该类别的值的表
我的问题是,当 JQuery 开始解析 XML 时,它在一个函数中进行解析(在我见过的所有示例中),所以我不知道如何在一个函数内部设置一个变量,并在用于打开该函数的下一个函数内部使用它。第二个文件,或第三个文件。
这是我现在所拥有的: 复制代码
<script>
var catid = ""; // I thought this, being outside of the function would be a global varaible
OA1 = ""; // I tried it with and without var in front
var OAid = "";
$(document).ready(function(){ //When opening an XML we do it in a function
$.ajax({
type: "GET",
url: "xml/categories.xml",
dataType: "xml",
success: function(xml) {
$(xml).find('Cat').each(function(){
//Next 2 rows don't matter b/c I can't use their values outside
//of the function
var catid = $(this).find('Catid').text();
var OA1 = $(this).find('OA1').text();
$('<div class="page-wrap"></div>').html('<table><tr><td>' + catid +'</td><td>OA1 '+ OA1 +'</td></tr></table></div>').appendTo('#page-wrap');
});
}
});
});
//The only way I know how to open up the next XML, start all over again
$(document).ready(function(){
$.ajax({
type: "GET",
url: "xml/OA.xml",
dataType: "xml",
success: function(xml) {
$(xml).find('OAData').each(function(){
var OAid = $(this).find('OAid').text();
$('<div class="page-wrap"></div>').html('<table><tr><td>OA ID is '+ OAid +'</td></tr></table></div>').appendTo('#page-wrap');
});
}
});
});
</script>
//Somebody shoot me
任何建议将不胜感激 - 我什至无法考虑变量问题的比较操作 b/c。
有没有办法比较 2 个 XML 文件,而我只是缺少它,或者您可以推荐使用某种临时位置的解决方案吗?
I'm trying to wrap my head around alternatives for global variables.
Case in question is one where I need to find values in one XML and compare against another XML (or more). Since the XML JQuery is, itself, a function and the operations beneath that are functions inside of a function (ugh) I can't get a value from 2 functions deep and use it globally.
So it's presently not possible for me to get an XML value from 1 file and use it filter another XML file, and that's where I need help.
I've been handed 3 XML files.
File 1 - categories.xml - contains a mapping of categories
ex...
<CAT>
<OA1>True</OA1>
<OA2>False</OA2>
<OA3>True</OA3>
<EP1>True</EP1>
<EP2>False</EP2>
<EP3>False</EP3>
</CAT>
File 2 = oa.xml - contains the values of each OA record
ex...
<OA>
<Name>Name 1</Name>
<City>City</City>
<State>ST</State>
</OA>
and so on...
File 3 = EP.xml - contains the values of each EP record
Copy code
<EP>
<object 1></object1>
<object 2></object2>
<object 3></object3>
</EP>
Now, what I thought I could do when I started was to allow the user to select a category, and based upon that selection return 2 tables containing the values that mapped to that category.
My problem is that when JQuery starts parsing XML it does it in a function (in all examples I've seen) so I have no idea how to set a variable inside of one function and use it inside of the next function used to open the 2nd file, or the 3rd.
Here is what I have now:
Copy code
<script>
var catid = ""; // I thought this, being outside of the function would be a global varaible
OA1 = ""; // I tried it with and without var in front
var OAid = "";
$(document).ready(function(){ //When opening an XML we do it in a function
$.ajax({
type: "GET",
url: "xml/categories.xml",
dataType: "xml",
success: function(xml) {
$(xml).find('Cat').each(function(){
//Next 2 rows don't matter b/c I can't use their values outside
//of the function
var catid = $(this).find('Catid').text();
var OA1 = $(this).find('OA1').text();
$('<div class="page-wrap"></div>').html('<table><tr><td>' + catid +'</td><td>OA1 '+ OA1 +'</td></tr></table></div>').appendTo('#page-wrap');
});
}
});
});
//The only way I know how to open up the next XML, start all over again
$(document).ready(function(){
$.ajax({
type: "GET",
url: "xml/OA.xml",
dataType: "xml",
success: function(xml) {
$(xml).find('OAData').each(function(){
var OAid = $(this).find('OAid').text();
$('<div class="page-wrap"></div>').html('<table><tr><td>OA ID is '+ OAid +'</td></tr></table></div>').appendTo('#page-wrap');
});
}
});
});
</script>
//Somebody shoot me
ANY ADVICE would be most appreciated - I haven't been able to even consider the compare operation b/c of the variable issue.
Is there a way to compare 2 XML files and I'm just missing it, or can you recommend a solution using some sort of temporary location?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因此,采纳 @Kevin B 的建议并对其进行一些增强,如果将这些函数分解为单独的函数,则可以轻松地将值传递给不同的成功处理函数。
因此,在您的
$(document).ready()
的ajax
调用的success
处理程序中,您创建了第二个ajax
按照@Kevin B 的建议进行调用。您可以通过将函数调用包装在成功处理程序中来向其传递附加数据。我将传递第二个嵌套函数调用(在getCategoriesSuccess
内)在第一个调用中所需的数据,以便它可用于第二个调用。这就是为什么我在第一个嵌套函数调用中传递 OAid 的原因,因为在 getOASuccess 内部需要它。我确信还有其他方法可以做到这一点,但这为您的成功处理程序提供了一些灵活性。
我希望这有帮助。如果还有其他问题请告诉我,我会相应更新我的答案。祝你好运!
So taking the suggestion of @Kevin B and enhancing it a bit, you can pass values to your different success handler functions easily if you factor those functions out into separate functions.
So inside your
$(document).ready()
'sajax
call'ssuccess
handler, you make a secondajax
call as @Kevin B suggested. You can pass additional data to this by wrapping a function call inside your success handler. And I'll pass the data that a second nested function call (insidegetCategoriesSuccess
) needs in that first call so that it's available for the second call. So that's why I passOAid
in the first nested function call because it's needed inside ofgetOASuccess
.I'm sure there are other ways to do this, but this gives you a bit of flexiblity with your success handlers.
I hope this helps. Let me know if there are additional questions and I'll update my answer accordingly. Good luck!