2

参考 jQuery 模板,Rick Strahl 有一篇出色的文章,解释了这里的功能。在解释过程中,他继续说 jQuery 在某种程度上支持模板

在此示例中,模板与数组合并。当结果是一个数组时,模板会自动应用于每个数组项。如果您传递单个数据项——比如股票报价——模板的工作方式完全相同,但只应用一次。模板还可以访问 $data 项,它提供当前数据项和有关当前正在执行的模板的信息。这使得将上下文保持在模板本身的上下文中成为可能,也可以将上下文从父模板传递给非常强大的子模板。

我不太明白这是什么意思。有人可以帮我理解这一点吗?

4

2 回答 2

2

它的类似 asp:Repeater 在 asp.net 中。将数据数组放入其中,控件根据数组中的每个项目自行填充。在这种情况下,它只是使用 jquery + jQuery Data Link。

所以而不是使用

for(looping)
 $("body").append("<div>"+sometext+"</div>");

改成

<script id="stockTemplate" type="text/x-jquery-tmpl">    
<div>${objName}</div>
</script>
...and
$("#somediv").link(someJSON);
于 2010-12-11T18:58:58.710 回答
0

He's talking about nesting templates; Allowing one template to be used inside another template. He then goes on to explain how the context for the parent template can be passed to these child templates nested inside it.

Templates need variables passed to them in order to display dynamic information. These variables being passed, usually in an array or hash/dictionary, are often referred to as a "context". As Rick's article mentions, you're able to have a main context in your parent template, but also pass context to the child template.

于 2010-12-11T18:19:45.213 回答