<!DOCTYPE HTML Public "-//W3C//DTD XHTML 1.0 transitional//en" "http://www.w3.org/TR/xhtml1/DTD/ Xhtml1-transitional.dtd "><HTMLxmlns= "http://www.w3.org/1999/xhtml"><Head><Metahttp-equiv= "Content-type"content= "text/html; charset=gb2312" /><title>Untitled Document</title> </Head><Body><DivID= ' title '></Div><DivID= ' Product '></Div></Body><Scriptlanguage= "JavaScript"src= "Test.js" ></Script></HTML>
//JavaScript DocumentvarViewcommand= (function(){ varTpl={product: [' <div> ' , ' ', ' <p>{#text #}</p> ', ' </div> '].join (‘‘), title: [' <div class= ' main ' > ', ' , ' <p>{#tips #}</p> ' , ' </div> '].join (‘‘)}, HTML= ' '; functionformatestring (str,obj) {returnStr.replace (/\{# (\w+) #\}/g,function(match,key) {returnObj[key]; }) } varAction ={create:function(Data,view) {if(data.length) { for(vari=0,len=data.length;i<len;i++) {HTML+=formatestring (Tpl[view],data[i]); } } Else{html+=formatestring (Tpl[view],data); }}, display:function(Container,data,view) {if(data) { This. Create (Data,view); } document.getElementById (Container). InnerHTML=html; HTML= ' '; } } return functionExcute (msg) {Msg.param=object.prototype.tostring.call (msg.param) = = = "[Object Array]"?Msg.param: [Msg.param]; Action[msg.command].apply (Action,msg.param)}) (); varProductdata =[{src:' Command/02.jpg ', Text:Peach Blossom}, {src:' Command/03.jpg ', Text:Sun}, {src:' Command/04.jpg ', Text:Green}],titledata={title:' Summer Warmth ', tips:' A warm summer day ' }; Viewcommand ({command:' Display ', param: [' title ', Titledata, ' title ']}) Viewcommand ({command:' Create ', param: [{src:' Command/01.jpg ', Text:' 01 Green ' },' Product ']}) Viewcommand ({command:' Display ', param: [' Product ', Productdata, ' product '] })
JavaScript Command mode Test example