2012-01-19 105 views
1

我試圖圍繞全局變量的替代方案。替代全局變量?

有問題的案例是我需要在一個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文件,我只是想念它,或者你能推薦一個使用某種臨時位置的解決方案嗎?

+0

只需將第二個'$ .ajax'移至第一個成功回調的內部,即可以訪問這兩個變量的地方。 –

+0

就全局變量的替代而言,我會推薦data()方法http://api.jquery.com/jQuery.data/ – Johan

回答

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()ajax通話的success處理程序中,您撥打第二個電話ajax作爲@Kevin乙建議。你可以通過在你的成功處理程序中包裝一個函數調用來傳遞額外的數據。我將傳遞第二次嵌套函數調用(在getCategoriesSuccess之內)在第一次調用中需要的數據,以便它可用於第二次調用。所以這就是爲什麼我在第一個嵌套函數調用中通過OAid,因爲它需要在getOASuccess之內。

我相信還有其他的方法可以做到這一點,但是這會讓您在成功處理程序時有一點靈活性。

我希望這會有所幫助。讓我知道是否還有其他問題,我會相應地更新我的答案。祝你好運!