我正在使用Jquery模板來顯示傳入的JSON數據 我想將模板加載到可緩存的外部文件中。我該怎麼做呢?Jquery Templates
UPDATE
http://encosia.com/2010/12/02/jquery-templates-composite-rendering-and-remote-loading/
非常接近,我結束了,我只是用一個iframe中,而不是解決方案;
我正在使用Jquery模板來顯示傳入的JSON數據 我想將模板加載到可緩存的外部文件中。我該怎麼做呢?Jquery Templates
UPDATE
http://encosia.com/2010/12/02/jquery-templates-composite-rendering-and-remote-loading/
非常接近,我結束了,我只是用一個iframe中,而不是解決方案;
也許這段代碼可以幫助:
<script id="entry_show_template" defer type="text/html">
<div class="entry">
Age: <span class="age"></span> <a class="name" href="#"></a>
</div>
</script>
也許你可以使用這個代碼也帶有src屬性,腳本可以訪問o:
$('#entry_show_template')
似乎有幾種技術可以實現這一點,每種技術都有其優點/缺點。
1)使用內聯腳本塊。這會爲每個頁面加載帶來模板塊。如果父頁面不可緩存,則可能會使用多個模板沉重
2)通過外部JavaScript文件中的全局變量對象訪問模板。這是可緩存的,但模板字符串變得難以破譯
3)模板字符串是JSON響應的一部分。這引出了問題,爲什麼不服務器端模板?
4)使用靜態iframe和編制上的iframe裝載模板 - 這是緩存,模板是可讀/可編輯,同源策略可能是一個問題,如果靜態元素被放置在另一個域
我終於選擇了iframe方法,但我太缺乏經驗,無法意識到所有的缺陷。
感謝
我用對服務器端資源的Ajax調用,ASPX在這個例子中
$.ajax({
url: "myprog.aspx",
data: { whichTemplate: "template I'm Looking for" },
success: function(result) {
// result is the text string containing either a single template or a delimated list of templates
$.template('templatename', result);
} ,
dataType: "text"
});
見我的回答對jQuery templates - where should I put them?
除了the article from Dave Ward你在你的問題中提到,它從An Introduction to jQuery Templates, by Stephen Walther引用一些額外的技巧。特別是,它涵蓋了如何獲取模板一次並緩存其編譯版本。
鏈接已死亡。 – 2013-01-11 09:46:49
@SvenHecht感謝您的注意,我已更新鏈接到他們的新域名 – Veger 2013-01-11 09:49:25
鏈接再次死亡(仍?) – IARI 2014-11-09 12:09:24