基于HTML模板和JSON數(shù)據(jù)的JavaScript交互(移動(dòng)端)

字號(hào):


    這篇文章主要介紹了基于HTML模板和JSON數(shù)據(jù)的JavaScript交互(移動(dòng)端)的相關(guān)資料,需要的朋友可以參考下
    寫本文之前,我正在做一個(gè)基于Tab頁(yè)的訂單中心:
    每點(diǎn)擊一個(gè)TAB標(biāo)簽,會(huì)請(qǐng)求對(duì)應(yīng)狀態(tài)的訂單列表。之前的項(xiàng)目,我會(huì)在js里使用 + 連接符連接多個(gè)html內(nèi)容:
    var html = '';
    html += '<div>' + 
    '<div></div>'+
    '<div>還沒有訂單</div>'+
    '</div>';
    Html內(nèi)容少點(diǎn)還好,但是當(dāng)內(nèi)容多起來的時(shí)候,再使用這種方式,以后維護(hù)起來不方便,也影響美觀,可閱讀性差。
    突然想起來PHP模板的相關(guān)知識(shí),那么應(yīng)該也有類似JavaScript模板一說。由于為了快速使用在項(xiàng)目上,就去網(wǎng)上找了找。還真有:
    基于HTML模板和JSON數(shù)據(jù)的JavaScript交互 
    http://www.zhangxinxu.com/wordpress/2012/09/javascript-html-json-template/
    看了下文章,使用起來還是蠻簡(jiǎn)單的:
    1、準(zhǔn)備好html模板
    <textarea>
    <!--goods-block-->
    <div>
    <div>
    <a href="<?php echo site_url('order/detail/'. '$id$')?>">
    <div>
    <img src="$thumb$"/>
    </div>
    <div>
    <div>
    <p>訂單號(hào):$order_num$</p>
    <p>收件人:$cus_name$</p>
    <p>$create_time$</p>
    </div>
    <div>
    <p><span></span></p>
    <p>$flag_name$</p>
    </div>
    </div>
    <div></div>
    </a>
    </div>
    </div>
    <!--/goods-block-->
    </textarea>
    <textarea>
    <!-- no order -->
    <div>
    <div></div>
    <div>還沒有訂單</div>
    </div>
    <!-- /no order -->
    </textarea>
    其中變量部分全部用 variate variate 表示。
    2、模板方法很簡(jiǎn)單,直接寫一個(gè)基于字符串原型的擴(kuò)展方法,確保全局可用:
    String.prototype.temp = function(obj) {
    return this.replace(/\$\w+\$/gi, function(matchs) {
    var returns = obj[matchs.replace(/\$/g, "")]; 
    return (returns + "") == "undefined"? "": returns;
    });
    };
    主要使用到了正則知識(shí)。
    3、準(zhǔn)備json數(shù)據(jù):
    {
    "ecd": 0,
    "msg": "成功",
    "result": [{
    "id": "32",
    "order_num": "test-001",
    "title": "test",
    "thumb": "http:\/\/40DA1265-40F6-D622-8BA5-04BA0AF72573.jpg",
    "item_id": "21",
    "price": "0.11",
    "cus_name": "test",
    "cus_tel": "10086",
    "cus_address": "北京 北京海淀區(qū)",
    "flag": "5",
    "create_time": "20160329115544",
    "update_time": "20160330120001",
    "flag_name": "訂單已取消"
    }],
    "locate": ""
    }
    4、使用ajax顯示數(shù)據(jù)
    $.progress_show('正在努力加載中');
    $.ajax({
    url: site_url + 'api/order/getAll/' + status,
    type: 'get',
    dataType: 'json',
    error: doAjax.error,
    success: function (response) {
    $.progress_hide();
    if (response.ecd == '0') {
    var htmlList = '', htmlTemp = $("textarea.js-order-tmp").val();
    if(typeof response.result === 'undefined'){
    htmlList = $("textarea.js-no-order-tmp").val();
    }else{
    $.each(response.result, function(i,el) {
    htmlList += htmlTemp.temp(el);
    });
    }
    $('.js-status-' + status).empty().append(htmlList);
    return true;
    } else {
    return $.alert(response.msg);
    }
    },
    });
    這里的部分方法沒有給出,大家知道流程、原理即可。通過點(diǎn)擊TAB標(biāo)簽,就可以顯示數(shù)據(jù)了:
    以上內(nèi)容是小編給大家介紹的基于HTML模板和JSON數(shù)據(jù)的JavaScript交互(移動(dòng)端),希望對(duì)大家有所幫助!