RIAWork進(jìn)展之Velocity For Js

          先簡單的做了一個,結(jié)合TrimPath提供的<a href=http://trimpath.com/project/wiki/JavaScriptTemplates target="_blank">JavascriptTemplate</a>實現(xiàn),目前的解決方案比較丑陋,通過xmlHttpRequest從服務(wù)器端獲取模板文件,然后交由JavascriptTemplate結(jié)合數(shù)據(jù)解析形成最后的html,示例如下:

          < html >
          < head >
          ????
          < script? src ="js/prototype.js" ></ script >
          ????
          < script? src ="js/template.js" ></ script >
          ????
          < script? src ="js/jstemplate.js" ></ script >
          </ head >
          < body >
          ????
          < span? id ="jmResult" ></ span >
          < script >
          ????
          var ?tdata = {User: {username: " jerry " } } ;
          ????JsTemplate.render("
          http://localhost:8080/RIAWork/test.jm " ,tdata, " jmResult ");
          </ script >
          </ body >
          </ html >
          模板文件如下:
          Hello?world,<font?color=red>${User.username}</font>
          從引用的js可以看出這個解決方案依賴于prototype.js、template.js以及jstemplate.js,其中prototype.js不用說,大名鼎鼎,template.js就是TrimPath提供的JavascriptTemplate,jstemplate.js則是由RIAWork提供的。
          jstemplate.js源碼如下:
          var?JSTEMPLATE_ELE_ID="__JSTEMPLATE_HIDDEN__";

          var?JsTemplate=Class.create();

          JsTemplate.prototype
          ={

          ????initialize:
          function(){
          ????????;
          ????}


          }


          JsTemplate.renderTemplateText
          =function(request,data,containerId){
          ????
          ????
          function?getEle(){
          ????????
          var?templateHiddenEle=$(JSTEMPLATE_ELE_ID);
          ????????
          if(templateHiddenEle==null){
          ????????????templateHiddenEle
          =document.createElement("span");
          ????????????templateHiddenEle.id
          =JSTEMPLATE_ELE_ID;
          ????????????templateHiddenEle.style.display
          ="none";
          ????????????document.body.insertBefore(templateHiddenEle);
          ????????}

          ????????
          return?templateHiddenEle;
          ????}

          ????
          ????
          var?templateText=request.responseText;
          ????getEle().innerText
          =templateText;
          ????
          var?templateObj=TrimPath.parseDOMTemplate(JSTEMPLATE_ELE_ID);
          ????
          var?result=templateObj.process(data);
          ????document.body.removeChild(getEle());
          ????$(containerId).innerHTML
          =result;
          }


          JsTemplate.render
          =function(templateUrl,data,containerId){
          ????
          var?templateAjax=new?Ajax.Request(templateUrl,{method:'get',asynchronous:'true',onComplete:function?callback(request){JsTemplate.renderTemplateText(request,data,containerId)}});
          }

          這個方案不好的地方就是在獲取到服務(wù)器返回的數(shù)據(jù)之后,將數(shù)據(jù)結(jié)合模板進(jìn)行渲染的時候還需要再連接到服務(wù)器獲取模板文件,盡管最后的渲染是在前端通過JavascriptTemplate做的。

          posted on 2006-05-16 17:09 BlueDavy 閱讀(2033) 評論(0)  編輯  收藏 所屬分類: @RIAWork

          公告

           









          feedsky
          抓蝦
          google reader
          鮮果

          導(dǎo)航

          <2006年5月>
          30123456
          78910111213
          14151617181920
          21222324252627
          28293031123
          45678910

          統(tǒng)計

          隨筆分類

          隨筆檔案

          文章檔案

          Blogger's

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 涿州市| 军事| 海淀区| 贞丰县| 汉川市| 元谋县| 锦屏县| 济宁市| 肥西县| 枣强县| 安陆市| 曲水县| 万载县| 青铜峡市| 治县。| 密云县| 平泉县| 文成县| 凌源市| 云安县| 安国市| 吉安县| 寿光市| 化州市| 永靖县| 福清市| 新密市| 昆山市| 亳州市| 渭源县| 金沙县| 梁山县| 含山县| 凤庆县| 宿松县| 甘孜| 临沂市| 于田县| 高邑县| 怀宁县| 清远市|