最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

基于jQuery的JavaScript模版引擎JsRender使用指南

 更新時間:2014年12月29日 14:20:15   投稿:hebedich  
這篇文章主要介紹了基于jQuery的JavaScript模版引擎JsRender使用指南,需要的朋友可以參考下

前言

     JsRender是一款基于jQuery的JavaScript模版引擎,它具有如下特點:

          ·  簡單直觀

          ·  功能強大

          ·  可擴展的

          ·  快如閃電

     這些特性看起來很厲害,但幾乎每個模版引擎,都會這么宣傳。。。

     由于工作需要,小菜才接觸到此款模版引擎。使用了一段時間,發(fā)現它確實比較強大,但小菜覺得有些地方強大的過頭了,反倒讓人覺得很難理解。

     另一方面,JsRender的官方文檔比較詳細,但其他資料出奇的少,遇到點什么問題,基本搜不到,不僅僅是相關問題搜不到,幾乎就是沒有結果。

     再加上JsRender有些地方確實是不好理解,所以急需小菜分享一些“最佳實踐”。

     基于最近一段時間的使用,小菜總結了一些實用經驗,當然,這些經驗在官方文檔上是找不到的。

     注意:本文不是基礎入門教程,以下例子中自帶注釋,不做過多說明,讀者自行體會,不懂的地方可以留言。

 嵌套循環(huán)使用#parent訪問父級數據(不推薦)

復制代碼 代碼如下:

 <!DOCTYPE html>
 <html>
   <head>
     <meta charset="utf-8">
     <title>嵌套循環(huán)使用#parent訪問父級數據 --- by 楊元</title>
     <style>
     </style>
   </head>
   <body>
     <div>
       <table>
         <thead>
           <tr>
             <th width="10%">序號</th>
             <th width="10%">姓名</th>
             <th width="80%">家庭成員</th>
           </tr>
         </thead>
         <tbody id="list">
         </tbody>
       </table>
     </div>
     <script src="jquery.min.js"></script>
     <script src="jsviews.js"></script>
     <!-- 定義JsRender模版 -->
     <script id="testTmpl" type="text/x-jsrender">
       <tr>
         <td>{{:#index + 1}}</td>
         <td>{{:name}}</td>
         <td>
           {{for family}}
             {{!-- 利用#parent訪問父級index --}}
             <b>{{:#parent.parent.index + 1}}.{{:#index + 1}}</b>
             {{!-- 利用#parent訪問父級數據,父級數據保存在data屬性中 --}}
             {{!-- #data相當于this --}}
             {{:#parent.parent.data.name}}的{{:#data}}
           {{/for}}
         </td>
       </tr>
     </script>
     <script>
       //數據源
       var dataSrouce = [{
         name: "張三",
         family: [
           "爸爸",
           "媽媽",
           "哥哥"
         ]
       },{
         name: "李四",
         family: [
           "爺爺",
           "奶奶",
           "叔叔"
         ]
       }];
       //渲染數據
       var html = $("#testTmpl").render(dataSrouce);
       $("#list").append(html);
     </script>
   </body>
 </html>

嵌套循環(huán)使用參數訪問父級數據(推薦)

復制代碼 代碼如下:

 <!DOCTYPE html>
 <html>
   <head>
     <meta charset="utf-8">
     <title>嵌套循環(huán)使用參數訪問父級數據 --- by 楊元</title>
     <style>
     </style>
   </head>
   <body>
     <div>
       <table>
         <thead>
           <tr>
             <th width="10%">序號</th>
             <th width="10%">姓名</th>
             <th width="80%">家庭成員</th>
           </tr>
         </thead>
         <tbody id="list">
         </tbody>
       </table>
     </div>
     <script src="jquery.min.js"></script>
     <script src="jsviews.js"></script>
     <!-- 定義JsRender模版 -->
     <script id="testTmpl" type="text/x-jsrender">
       <tr>
         <td>{{:#index + 1}}</td>
         <td>{{:name}}</td>
         <td>
           {{!-- 使用for循環(huán)時,可以在后邊添加參數,參數必須以~開頭,多個參數用空格分隔 --}}
           {{!-- 通過參數,我們緩存了父級的數據,在子循環(huán)中通過訪問參數,就可以間接訪問父級數據 --}}
           {{for family ~parentIndex=#index ~parentName=name}}
             <b>{{:~parentIndex + 1}}.{{:#index + 1}}</b>
             {{!-- #data相當于this --}}
             {{:~parentName}}的{{:#data}}
           {{/for}}
         </td>
       </tr>
     </script>
     <script>
       //數據源
       var dataSrouce = [{
         name: "張三",
         family: [
           "爸爸",
           "媽媽",
           "哥哥"
         ]
       },{
         name: "李四",
         family: [
           "爺爺",
           "奶奶",
           "叔叔"
         ]
       }];
       //渲染數據
       var html = $("#testTmpl").render(dataSrouce);
       $("#list").append(html);
     </script>
   </body>
 </html>

 自定義標簽(custom tag)中使用else(強烈不推薦)

復制代碼 代碼如下:

 <!DOCTYPE html>
 <html>
   <head>
     <meta charset="utf-8">
     <title>自定義標簽中使用else --- by 楊元</title>
     <style>
     </style>
   </head>
   <body>
     <div>
       <table>
         <thead>
           <tr>
             <th width="50%">名稱</th>
             <th width="50%">單價</th>
           </tr>
         </thead>
         <tbody id="list">
         </tbody>
       </table>
     </div>
     <script src="jquery.min.js"></script>
     <script src="jsviews.js"></script>
     <!-- 定義JsRender模版 -->
     <script id="testTmpl" type="text/x-jsrender">
       <tr>
         <td>{{:name}}</td>
         <td>
           {{!-- isShow為自定義標簽,price是傳入的參數,status是附加屬性 --}}
           {{isShow price status=0}}
             {{:price}}
           {{else price status=1}}
             --
           {{/isShow}}
         </td>
       </tr>
     </script>
     <script>
       //數據源
       var dataSrouce = [{
         name: "蘋果",
         price: 108
       },{
         name: "鴨梨",
         price: 370
       },{
         name: "桃子",
         price: 99
       },{
         name: "菠蘿",
         price: 371
       },{
         name: "橘子",
         price: 153
       }];
       //自定義標簽
       $.views.tags({
         "isShow": function(price){
           var temp=price+''.split('');
           if(this.tagCtx.props.status === 0){
             //判斷價格是否為水仙花數,如果是,則顯示,否則不顯示
             if(price==(Math.pow(parseInt(temp[0],10),3)+Math.pow(parseInt(temp[1],10),3)+Math.pow(parseInt(temp[2],10),3))){
               return this.tagCtxs[0].render();
             }else{
               return this.tagCtxs[1].render();
             }
           }else{
             return "";
           }
         }
       });
       //渲染數據
       var html = $("#testTmpl").render(dataSrouce);
       $("#list").append(html);
     </script>
   </body>
 </html>

用helper代替自定義標簽(推薦)

復制代碼 代碼如下:

 <!DOCTYPE html>
 <html>
   <head>
     <meta charset="utf-8">
     <title>用helper代替自定義標簽 --- by 楊元</title>
     <style>
     </style>
   </head>
   <body>
     <div>
       <table>
         <thead>
           <tr>
             <th width="50%">名稱</th>
             <th width="50%">單價</th>
           </tr>
         </thead>
         <tbody id="list">
         </tbody>
       </table>
     </div>
     <script src="jquery.min.js"></script>
     <script src="jsviews.js"></script>
     <!-- 定義JsRender模版 -->
     <script id="testTmpl" type="text/x-jsrender">
       <tr>
         <td>{{:name}}</td>
         <td>
           {{!-- 利用原生的if做分支跳轉,利用helper做邏輯處理 --}}
           {{if ~isShow(price)}}
             {{:price}}
           {{else}}
             --
           {{/if}}
         </td>
       </tr>
     </script>
     <script>
       //數據源
       var dataSrouce = [{
         name: "蘋果",
         price: 108
       },{
         name: "鴨梨",
         price: 370
       },{
         name: "桃子",
         price: 99
       },{
         name: "菠蘿",
         price: 371
       },{
         name: "橘子",
         price: 153
       }];
       //Helper
       $.views.helpers({
         "isShow": function(price){
           var temp=price+''.split('');
           if(price==(Math.pow(parseInt(temp[0],10),3)+Math.pow(parseInt(temp[1],10),3)+Math.pow(parseInt(temp[2],10),3))){
             return true;
           }else{
             return false;
           }
         }
       });
       //渲染數據
       var html = $("#testTmpl").render(dataSrouce);
       $("#list").append(html);
     </script>
   </body>
 </html>

演示代碼打包下載:http://xiazai.jb51.net/201412/yuanma/JsRender_Demo(jb51.net).rar

相關文章

最新評論

宁明县| 内江市| 泽州县| 鸡西市| 隆回县| 邮箱| 南充市| 永平县| 佛学| 古田县| 江油市| 璧山县| 麻栗坡县| 迁西县| 大安市| 丁青县| 济南市| 子洲县| 大田县| 嘉鱼县| 门源| 百色市| 天长市| 宜黄县| 高安市| 武夷山市| 永年县| 霍城县| 乐业县| 灵山县| 富锦市| 同仁县| 莱芜市| 礼泉县| 兴安县| 武功县| 茌平县| 德格县| 磐安县| 新巴尔虎右旗| 西丰县|