JQuery 模板 - 调用 ${$item.function()} 的次数多于模板中指定的次数
Jquery-tmpl 似乎多次调用您附加到 item
的函数。
JSfiddle:http://jsfiddle.net/abQwc/2/
对于模板呈现的每个项目,console.log 的 show rowCount 被调用 4 次。
模板:
<h1 class="${$item.rowCount() % 2 == 0 ? "even" : "odd"}">${Name} - ${Payload}</h1
数据:
data = [
{ Name: "1", Payload: "Data1" },
{ Name: "2", Payload: "Data2" },
{ Name: "3", Payload: "Data3" }
]
脚本:
$(function() {$( "#template" )
.tmpl(data, {
rowCount: function(){
var rowCount = 0;
return function(){
console.log(this.data.Payload);
return ++rowCount;
}
}()
})
.appendTo( "body" )})
为什么?我已经想出了如何绕过它,但它散发着巫术的味道。
Jquery-tmpl seems to call functions you attach to item
multiple times.
JSfiddle: http://jsfiddle.net/abQwc/2/
The console.log's show rowCount got called 4 times for each item rendered by the template.
Template:
<h1 class="${$item.rowCount() % 2 == 0 ? "even" : "odd"}">${Name} - ${Payload}</h1
Data:
data = [
{ Name: "1", Payload: "Data1" },
{ Name: "2", Payload: "Data2" },
{ Name: "3", Payload: "Data3" }
]
Script:
$(function() {$( "#template" )
.tmpl(data, {
rowCount: function(){
var rowCount = 0;
return function(){
console.log(this.data.Payload);
return ++rowCount;
}
}()
})
.appendTo( "body" )})
Why? I've already figured out how to get around it, but it reeks of witchcraft.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我替换:
并
能够让它工作。然而,Options 参数仍然为每个项目执行 4 次。我相信 tmpl 中的计算充其量仍然是不确定的。
I replaced:
with
and was able to get it to work. However, the Options parameter was still getting executed 4 times for each item. I believe calculations like that in tmpl are still iffy at best.