显然,jQuery的模板可以被编译,它有助于与if语句显示here为模板的性能。
但如here所示,预编译的jQuery模板对我的情况并没有太大的作用,因为我的模板不包含逻辑块。
对于那些建议使用另一个模板引擎的人,理想情况下我想只使用jQuery模板,因为团队中的每个人都知道jQuery。还有一些this测试用例比较了几个模板引擎。
嗨,
就在今天,我被告知,有与使用jQuery模板的性能问题。
为了进行比较,我使用了jQuery模板和良好的旧字符串append方法将行添加到表中。结果可以看到here。与字符串附加方法相比,使用jQuery模板大约慢65%,Ouch!
我想知道怎样才能提高jQuery模板脚本的性能。
完整的脚本可以在提供的链接中查看。但基本思路如下:
模板:
<script type="x-jquery-tmpl" id="tmplRow">
<tr>
<td><input type="checkbox" value="${id}" /></td>
<td>${firstName} ${lastName}</td>
<td class="edit">
<a>Edit</a>
<input style="display:none;" type="hidden" value="Blah" />
<input class="cancel" type="button" value="cancel" />
</td>
<td class="select">
<a>Select</a>
<select style="display:none;">
<option>0</option>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
<option>6</option>
<option>7</option>
<option>8</option>
<option>9</option>
<option>10</option>
</select>
<input class="cancel" type="button" value="cancel" />
</td>
<td>More string</td>
<td>More string</td>
<td>More string</td>
<td>More string</td>
<td>More string</td>
<td>More string</td>
</tr>
</script>
数据:
<script type="text/javascript">
var data = [];
for (var i = 0; i < 100; i++) {
var row = {
id: i,
firstName: 'john',
lastName: 'doe'
};
data.push(row);
}
</script>
HTML:
<table id="table"></table>
执行:
<script type="text/javascript">
$('#tmplRow').tmpl(data).appendTo('#table');
</script>
个
谢谢,
驰
我不介意检查车把。但他们的下载不起作用。如果我仅从源代码引用“handlebars.js”,它将会以未定义的方法“required”失败。你有没有js文件的副本? – 2011-01-06 16:00:31
jquery模板'编译'模板以及... – user406905 2011-01-06 22:27:55
显然,'编译'模板只有在有逻辑控制块时才有帮助。详细信息请查看更新后的问题。 – 2011-01-06 22:41:40