Discuz!官方免费开源建站系统

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索

[分享] 基于 JS artTemplate 模板引擎 Template 加载的优化

[复制链接]
monkeye 发表于 2015-3-16 15:40:19 | 显示全部楼层 |阅读模式
artTemplate (http://aui.github.io/artTemplate)一种全新的 javascript 模板引擎,它采用了预编译的方式让性能有了质的飞跃,并充分利用 javascript 引擎特性,使得其性能无论在前端还是后端都有极其出色的表现。但是在实际应用中我们发现,和Ajax配合后通常会产生很多的 Template,这些 Template 都堆积在页面源码中时间一长难免会越来越多,影响页面加载的字节数,让页面越来越大。
我们对此进行了优化,由于我们的业务主要是针对移动产品的,因此我们可以放心的使用 HTML5 的 localStorage 进行缓存操作,缓存部分代码如下:
  1. //实现template文件的缓存
  2. var TC = {
  3.     VERSION : "xxxxx" ,
  4.     KEYPREFIX : "xxx_",

  5.     //加载js文件
  6.     load : function(filePath ,version ,params) {

  7.         if (arguments.length == 2 && typeof(arguments[1]) == 'object') {
  8.             params = arguments[1];
  9.             version = null;
  10.             tag = null;
  11.         };

  12.         var source = TC.getCacheTSData(filePath ,version);
  13.         if(!source) {
  14.             return;
  15.         }

  16.         if(params) {
  17.             source = source.replace('[%params%]',JSON.stringify(params));
  18.         }

  19.         jQuery(source).appendTo('body');
  20.     } ,
  21.     //请求js文件
  22.     getCacheTSData : function(filePath ,version) {
  23.         var source = null;
  24.         if(version) { //获得版本号
  25.             source = localStorage.getItem(TC.KEYPREFIX + filePath);
  26.             if(source) {
  27.                 if(version == localStorage.getItem(TC.KEYPREFIX + filePath + "version")) {
  28.                     return source; //版本号一致直接返回
  29.                 }
  30.                 localStorage.removeItem(TC.KEYPREFIX + filePath);
  31.                 localStorage.removeItem(TC.KEYPREFIX + filePath + "version");
  32.             }
  33.         }
  34.         //如果没有缓存则网络请求js
  35.         jQuery.ajax({ //同步请求js文件
  36.             async : false,
  37.             cache : false ,
  38.             dataType: "text",
  39.                 url: filePath,
  40.                 success: function(data) {
  41.                     source = data;
  42.                 }
  43.             });
  44.          //缓存js
  45.         if(source && version) {
  46.             var i = 2;
  47.             while(i--) {
  48.                 try {
  49.                     localStorage.setItem(TC.KEYPREFIX + filePath ,source);
  50.                     localStorage.setItem(TC.KEYPREFIX + filePath + "version" ,version);
  51.                     break;
  52.                 } catch(err) {
  53.                     if(err.name == 'QuotaExceededError') {
  54.                         localStorage.clear(); //如果缓存已满则清空
  55.                     }
  56.                 }
  57.             }
  58.         }
  59.         return source;
  60.     }
  61. };
复制代码
这时,我们可以把诸多的 Template 分门别类的存放在不同的文件中,比如把首页的 Template 代码存放在 tpl_index.html 中,列表页的 Template 代码存放在 tpl_list.html 中。每个文件中可以放多个 Template 定义,文件内容如下:
  1. <script id="header" type="text/html">
  2. <div class="header"><h3><%= header.name %></h3></div>
  3. </script>

  4. <script id="hotList" type="text/html">
  5. <ul>
  6.     <% for(i = 0; i < list.length; i++){ %>
  7.         <li tid="<%= list[i].tid %>"><%= list[i].subject %></li>
  8.     <% } %>
  9. </ul>
  10. </script>

  11. ······
复制代码
在我们使用的时候,直接引用文件即可,如下:
  1. TC.load("tmpl/tpl_list.html");
复制代码
首次访问此函数时,会调用 tmpl/tpl_list.html 文件并缓存到 localStorage 中,以后会直接从 localStorage 中获取 Template 的内容,直到 TC.VERSION 中的值变化,才会重新调用文件。从而减少了主页面请求的字节数。
用过 artTemplate 的人会习惯直接把一些内容很短的模板直接写在 JS 里,类似下面
  1. template.compile('header', '<div class="header"><h3><%= header.name %></h3></div>');
复制代码
当然这样更省事。
此方案的是保证减少页面请求字节及页面请求数的目的下还能做到方便维护而提供的!

此功能在打通版微社区中实现!

北北″ 发表于 2015-3-16 15:41:11 | 显示全部楼层
回复

使用道具 举报

湖中沉 发表于 2015-3-16 15:41:59 | 显示全部楼层
猴哥威武
回复

使用道具 举报

问鼎传媒 发表于 2015-3-16 19:35:03 | 显示全部楼层
学习了!
回复

使用道具 举报

nestfly 发表于 2015-3-27 11:02:12 | 显示全部楼层
不错,不错。。。
回复

使用道具 举报

五格子fivegrid 发表于 2015-3-27 11:05:05 | 显示全部楼层
棒棒哒
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|Discuz! 官方站 ( 皖ICP备16010102号 )star

GMT+8, 2024-5-3 23:19 , Processed in 0.102777 second(s), 22 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2023, Tencent Cloud.

快速回复 返回顶部 返回列表