<script type="text/blade_config"> { url: 'restful/index', template: 'indexTmpt' }</script>
<script id="indexTmpt" type="text/blade_template"> <ul> <% for(var i = 0, len = data.length; i < len; i++) { %> <li><%=data[i].name %></li> <% } %> </ul></script>
//模拟数据返回data = [ {id: 0, name: 'item_0'}, {id: 1, name: 'item_1'}, {id: 2, name: 'item_2'}]
① 解析页面的config信息,取出url以及template
② 根据url发出请求返回数据,这里由于是局域网应该很快
③ 解析template,根据data生成静态html
④ 其它处理,返回客户端
我们这里略去url请求一步,假设数据已经返回,否则这里又要写.net程序
var data = [ {id: 0, name: 'item_0'}, {id: 1, name: 'item_1'}, {id: 2, name: 'item_2'}];
这里的模板字符串为:
var template = [ '<ul>', '<% for(var i = 0, len = data.length; i < len; i++) { %>', '<li><%=data[i].name %></li>', '<% } %>', '</ul>'].join('');
然后我们要做的就是解析这个模板,生成对应的模板解析函数,这里是调试代码:
var data = [ { id: 0, name: 'item_0' }, { id: 1, name: 'item_1' }, { id: 2, name: 'item_2' }];var template = [ '<ul>', '<% for(var i = 0, len = data.length; i < len; i++) { %>', '<li><%=data[i].name %></li>', '<% } %>', '</ul>'].join('');var templateHandler = function (text, data) { var noMatch = /(.)^/; var escapes = { "'": "'", '//': '//', '/r': 'r', '/n': 'n', '/t': 't', '/u2028': 'u2028', '/u2029': 'u2029' }; var escaper = ///|'|/r|/n|/t|/u2028|/u2029/g; var templateSettings = { evaluate: /<%([/s/S]+?)%>/g, interpolate: /<%=([/s/S]+?)%>/g, escape: /<%-([/s/S]+?)%>/g }; var render; settings = templateSettings; var matcher = new RegExp([ (settings.escape || noMatch).source, (settings.interpolate || noMatch).source, (settings.evaluate || noMatch).source ].join('|') + '|$', 'g'); var index = 0; var source = "__p+='"; text.replace(matcher, function (match, escape, interpolate, evaluate, offset) { source += text.slice(index, offset) .replace(escaper, function (match) { return '//' + escapes[match]; }); if (escape) { source += "'+/n((__t=(" + escape + "))==null?'':escape(__t))+/n'"; } if (interpolate) { source += "'+/n((__t=(" + interpolate + "))==null?'':__t)+/n'"; } if (evaluate) { source += "';/n" + evaluate + "/n__p+='"; } index = offset + match.length; return match; }); source += "';/n"; if (!settings.variable) source = 'with(obj||{}){/n' + source + '}/n'; source = "var __t,__p='',__j=Array.PRototype.join," + "print=function(){__p+=__j.call(arguments,'');};/n" + source + "return __p;/n"; return source; try { render = new Function(settings.variable || 'obj', source); } catch (e) { e.source = source; throw e; } if (data) return render(data); var template = function (data) { return render.call(this, data); }; template.source = 'function(' + (settings.variable || 'obj') + '){/n' + source + '}'; return template;}templateHandler(template, data)View Code
首次调试爆了很多错误,而且服务器端的调试比较费力,错了只能靠经验去猜测
这里返回的是需要构造成函数的字符串,但是我们看到我们的“ul”等标签被吃掉了!!!
var __t,__p='',__j=Array.prototype.join,print=function(){__p+=__j.call(arguments,'');};with(obj||{}){__p+=''; for(var i = 0, len = data.length; i < len; i++) { __p+=''+((__t=(data[i].name ))==null?'':__t)+''; } __p+='';}return __p;
我们这里一旦调用就抛了一个错误,这个时候一般是模板或者传入数据出错了,可惜的是他是对其中一段语法不可解析!这里从侧面反映出一个问题:
该方法若是模板出错会导致程序无法运行,如果是node的话很可能就crash了!
PS:这里由于CLR4解析javascript的时候字符串的replace遇到正则时有问题,在此逗留3小时,这里把我搞惨了,定位就很久最后还得重写模板解析!!!
1 var data = [ 2 { id: 0, name: 'item_0' }, 3 { id: 1, name: 'item_1' }, 4 { id: 2, name: 'item_2' } 5 ]; 6 7 var template = [ 8 '<ul>', 9 '<% for(var i = 0, len = data.length; i < len; i++) { %>', 10 '<li><%=data[i].name %></li>', 11 '<% } %>', 12 '</ul>' 13 ].join(''); 14 15 var templateHandler = function (text, data) { 16 17 var noMatch = /(.)^/; 18 19 var escapes = { 20 "'": "'", 21 '//': '//', 22 '/r': 'r', 23 '/n': 'n', 24 '/t': 't', 25 '/u2028': 'u2028', 26 '/u2029': 'u2029' 27 }; 28 29 var escaper = ///|'|/r|/n|/t|/u2028|/u2029/g; 30 31 var templateSettings = { 32 evaluate: /<%([/s/S]+?)%>/g, 33 interpolate: /<%=([/s/S]+?)%>/g 34 }; 35 36 var render; 37 var settings = templateSettings; 38 39 var matcher = new Reg
新闻热点
疑难解答