全局变量的替代品?

时间:2012-01-19 19:59:17

标签: javascript jquery xml xml-parsing

我正试图围绕全局变量的替代方案。

有问题的案例是我需要在一个XML中查找值并与另一个XML(或更多)进行比较的案例。由于XML JQuery本身就是一个函数,而下面的操作是函数内部的函数(呃),我无法从2个函数中获取值并在全局中使用它。

所以目前我无法从1个文件中获取XML值并使用它来过滤另一个XML文件,这就是我需要帮助的地方。

我已经交了3个XML文件。

文件1 - categories.xml - 包含类别映射 EX ...

<CAT>
      <OA1>True</OA1>
      <OA2>False</OA2>
      <OA3>True</OA3>
      <EP1>True</EP1>
      <EP2>False</EP2>
      <EP3>False</EP3>
</CAT>

文件2 = oa.xml - 包含每个OA记录的值 EX ...

<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文件而我只是错过它,或者你能推荐使用某种临时位置的解决方案吗?

1 个答案:

答案 0 :(得分:1)

因此,考虑到@Kevin B的建议并稍微增强它,如果将这些函数分解为单独的函数,则可以轻松地将值传递给不同的成功处理函数。

<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');

                });
              $.ajax({
                type: "GET",
                url: "xml/OA.xml",
                dataType: "xml",
                success: function(xml) { getCategoriesSuccess(xml, catid, OA1, OAid); }
              });
            }
       });
      });

      function getCategoriesSuccess(xml, catid, OA1, OAid) {
        $(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');

        });
        $.ajax({
            type: "GET",
            url: "xml/OA.xml",
            dataType: "xml",
            success: function(xml) { getOASuccess(xml, OAid); }
          });
         });        
      }

      function getOASuccess(xml, OAid){
        $(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>

因此,在$(document).ready()的{​​{1}}来电ajax处理程序中,您可以按@Kevin B建议进行第二次success来电。您可以通过在成功处理程序中包装函数调用来将其他数据传递给此。并且我将在第一次调用中传递第二个嵌套函数调用(在ajax内)所需的数据,以便它可用于第二次调用。这就是我在第一个嵌套函数调用中传递getCategoriesSuccess的原因,因为它需要在OAid内。

我确信还有其他方法可以做到这一点,但这会让您对成功处理程序有一点灵活性。

我希望这会有所帮助。如果还有其他问题,请告诉我,我会相应地更新我的答案。祝你好运!