2012-11-03 118 views
2

我工作的地方,我需要緩存某些請求HTML5緩存清單緩存請求清單不

所以我是用HTML5緩存

這裏我的清單文件勞作應用定義

CACHE MANIFEST 
# 2d25a26de3a1148a2fa5e534325f84cca2184090174c6ba451451c54f71f52d6 
assets/application.js 
assets/application.css 
assets/glyphicons/png/glyphicons_064_lightbulb.png 
assets/jquery-mobile/ajax-loader.gif 
assets/jquery-mobile/icons-18-white.png 
application.manifest 

NETWORK: 
/project_show 
/application.manifest 

現在我有/projects_show頁面內容看起來像這樣

<div data-role="header" class="header"> 
</div> 

<div data-role="content" class="content"> 
</div> 

<div data-role="footer" class="footer" style="text-align:center"> 
</div> 

<script type="text/javascript"> 
    <%= store_key %> 
</script> 

<script id="header" type="text/template"> 
    <h1>Listing of {{ carName }}</h1> 
</script> 

<script id="content" type="text/template"> 
    <span>{{ pageName }}</span> 
</script> 

<script id="footer" type="text/template"> 
    <span> CopyRight &copy; {{ user }} </span> 
</script> 


<script type="text/javascript"> 

    $(document).ready(function() { 
     if (window.navigator.onLine) { 
       $.getJSON('/project/2.json',function(data) { 

        localStorage.setItem("acura",JSON.stringify(data)); 

         var carTemplate = $('#header').html(); 
         var pageTemplate = $('#content').html(); 
         var footerTemplate = $("#footer").html(); 
         $('div.header').html(Mustache.to_html(carTemplate, data)); 
        $('div.content').html(Mustache.to_html(pageTemplate, data)); 
       $('div.footer').html(Mustache.to_html(footerTemplate,data)); 

        }) 

     else { 
      var data = JSON.parse(localStorage.getItem("acura")) 
      var carTemplate = $('#header').html(); 
       var pageTemplate = $('#content').html(); 
       var footerTemplate = $("#footer").html(); 
      $('div.header').html(Mustache.to_html(carTemplate, data)); 
       $('div.content').html(Mustache.to_html(pageTemplate, data)); 
       $('div.footer').html(Mustache.to_html(footerTemplate,data)); 
     }    
    }) 
</script> 

可以看到的想法在這裏是如果用戶在網上從服務器獲取的json響應,如果沒有,那麼從localStorage

獲取數據不幸的是,上面的代碼是不是因爲工作,原因

一)緩存清單文件緩存ajax要求以及

現在一看到我還沒有問HTML緩存清單緩存json要求

/projects/2.json

誰能告訴我爲什麼會這樣

上面的代碼工作正常,如果GET要求,如果修改爲POST請求,即a POST request to /projects/2.json instead of GET

但是,這不是我「M在任何建議尋找

FYI清單確實按照清單文件正確,因爲我還發現我的/projects越來越緩存,即使不manifest

定義
+0

我太面臨這個問題,不知道是否可以通過'帖子'謝謝 – Viren

回答

0

嘗試將/project/2.json添加到清單的NETWORK部分。然後,該URL的請求應始終從服務器而不是從appcache提供。

關於你/projects頁面緩存得到:你綁定該頁面這個應用程序緩存(通過在頁面<html>元素的manifest="..."屬性)?如果將頁面綁定到appcache,頁面本身總是隱式地作爲appcache的一部分(作爲所謂的「主條目」)。

+0

我可能是一個選項,但我相信它不會長期運行,因爲問題也必須在rails中標記'ruby-on-rails''/ projects/2.json' 2可以被替換b Ÿ任何數字,如'/ projects/999.json'或'/ projects/1111。json'嘗試設置每個條目都很難,也不可能達到 – Viren

+0

「NETWORK」節中的條目使用前綴匹配,因此如果添加'/ projects /',則可以匹配所有這些json URL。 –

0

這也讓我發瘋。我發現的工作是添加到清單「」index.html#content「的NETWORK部分。您的工作方式我認爲您還需要添加」index.html#header「和」index.html#footer 「

我認爲發生了什麼是index.html被緩存,」內容「」標題「和」頁腳「是其中的一部分,因此調用加載這些部分(或在我的情況下頁)被困並且ajax調用沒有運行,因爲index.html被緩存了。通過添加index.html#內容到NETWORK部分,它強制呼叫。