jQueryプラグインjTemplateにおける時間変換について

7118 ワード

  jTemplate                       (json),
    /Date(1364478769713)/     ,                     。
   :
         ,      :
$("#result").setParam('DateFormat', DateFormat);

function DateFormat(p_date) {
    var date = new Date(parseInt(p_date.replace("/Date(", "").replace(")/", ""), 10));
    var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
    var currentDate = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
    return date.getFullYear() + "/" + month + "/" + currentDate;
}

      ,  :{$P.DateFormat($T.Item.StartTime)}

   :
    jTemplate   ,      :

      104-106//set handlers        ,         
this.f_cloneData = (this.settings.f_cloneData !== undefined) ? (this.settings.f_cloneData) : (TemplateUtils.cloneData);
this.f_escapeString = (this.settings.f_escapeString !== undefined) ? (this.settings.f_escapeString) : (TemplateUtils.escapeHTML);
this.f_parseJSON = (this.settings.f_parseJSON !== undefined) ? (this.settings.f_parseJSON) : ((this.settings.disallow_functions) ? (jQuery.parseJSON) : (TemplateUtils.parseJSON));

this.f_timeConvert = (this.settings.f_timeConvert !== undefined) ? (this.settings.f_timeConvert) : (TemplateUtils.timeConvert);

if (s == null) {
    return;
}

   319-323/**
* Process template and get the html string.
* @param {object} d data
* @param {object} param parameters
* @param {Element} element a HTML element
* @param {Number} deep
* @return {String}
*/
Template.prototype.get = function (d, param, element, deep) {
    ++deep;

    if (deep == 1 && element != undefined) {
        jQuery.removeData(element, "jTemplatesRef");
    }

    /*    /Date(...)/     */
    this.f_timeConvert(d);

  var $T = d, $P, ret = '';

    //create clone of data
    if (this.settings.clone_data) {
        $T = this.f_cloneData(d, { escapeData: (this.settings.filter_data && deep == 1), noFunc: this.settings.disallow_functions }, this.f_escapeString);
    }

    //create clone of parameters
    if (!this.settings.clone_params) {
        $P = jQuery.extend({}, this._param, param);
    } else {
        $P = jQuery.extend({},
            this.f_cloneData(this._param, { escapeData: (this.settings.filter_params), noFunc: false }, this.f_escapeString),
            this.f_cloneData(param, { escapeData: (this.settings.filter_params && deep == 1), noFunc: false }, this.f_escapeString));
    }

    for (var i = 0, l = this._tree.length; i < l; ++i) {
        ret += this._tree[i].get($T, $P, element, deep);
    }

    this.EvalObj = null;

    --deep;
    return ret;
};

   367 TemplateUtils      timeConvert  
/**
* Template utilities.
* @namespace Template utilities.
*/
TemplateUtils = function () {
};

/**
* ConvertTime to normal,
*     ,
*       /Date(1364478769713)/   
* 
*/
var regTime = /^\/Date\(/;
TemplateUtils.timeConvert = function (obj) {
    for (var i in obj) {
        if (regTime.test(obj[i])) {
            obj[i] = (new Date(parseInt(obj[i].replace("/Date(", "").replace(")/", ""), 10))).format("yyyy/MM/dd");
            //        format("yyyy/MM/dd")   ,      
        } else {
            if (typeof (obj[i]) === "object") {
                TemplateUtils.timeConvert(obj[i]);
            }
        }
    }
};

//---------Date.prototype.format--------------------//
/*       (BUG)       SSS,   2  ,      ,
       “00”+o[k]           ~*/
/**
*            ,
*     :(new Date()).format("yyyy/MM/dd");
*/
Date.prototype.format = function (format) {
    var o = {
        "M+": this.getMonth() + 1,  //month        
        "d+": this.getDate(),       //day        
        "h+": this.getHours(),      //hour        
        "m+": this.getMinutes(),    //minute        
        "s+": this.getSeconds(),    //second        
        "q+": Math.floor((this.getMonth() + 3) / 3), //quarter        
        "S+": this.getMilliseconds() //millisecond        
    };
    if (/(y+)/.test(format))
        format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(format)) format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
    return format;
};