我想用如下.
<script id="movieTemplate" type="text/x-jquery-tmpl">
<tr>
<td>{{=Title}}</td>
<td>
{{* var title = $ctx.GetTitleFunction($view);}}
{{#each Languages}}
<div>
<em>{{=Name}}</em>
<em>({{* title;}})</em>
</div>
{{/each}}
</td>
</tr>
</script>
<script type="text/javascript">
function GetTitle(data){
return "Title : " + data.Title;
}
var movies = [
{
Title: "Meet Joe Black",
Languages: [
{ Name: "English" },
{ Name: "French" }
]
},
{
Title: "Eyes Wide Shut",
Languages: [
{ Name: "French" },
{ Name: "Mandarin" },
{ Name: "Spanish" }
]
}
];
$.views.allowCode = true;
$.views.registerHelpers({ GetTitleFunction: GetTitle });
$( "#movieList" ).html(
$( "#movieTemplate" ).render( movies )
);
</script>
Run Code Online (Sandbox Code Playgroud)
我无法使用{{*title;}}或{{*= title}}来呈现javascript变量.
如何在javascript变量中呈现值?
Joh*_*apa 11
我创建了一个小提琴,向您展示如何使用辅助函数返回标题.它使用新的beta候选语法组合了辅助函数和视图路径.我不需要使用allowCode来做这个... allowCode让你定义变量和进程逻辑,但它不能完全访问给定的javascript(为了安全).无论如何,我认为没有allowCode,代码更清晰,但是你打电话:)
http://jsfiddle.net/johnpapa/LqchY/
请注意,Boris发布的新更改:http://www.borismoore.com/2012/03/approaching-beta-whats-changing-in_06.html
下面的代码有这些显着的变化:
我希望这有帮助!
<script id="movieTemplate" type="text/x-jsrender">
<tr>
<td>{{:Title}}</td>
<td>
<!-- Use the helper function to display the title -->
{{:~GetTitleFunction()}}
{{for Languages}}
<div>
<em>{{:Name}}</em>
<!-- Use the view path for parent. could also do #view.parent.parent.data -->
<em>( {{:#parent.parent.data.Title}} )</em>
<!-- Use the helper function and view path -->
<em>{{:~GetTitleFunction(#parent.parent.data)}}</em>
</div>
{{/for}}
</td>
</tr>
</script>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8992 次 |
| 最近记录: |