jQuery 模板注入 ;在每个 foreach 项目上?为什么?

发布于 2024-12-12 05:17:44 字数 898 浏览 0 评论 0原文

我正在使用带有 Knockout 的 jQuery 模板,由于某种原因,当使用 foreach 时, 会神奇地插入到每个项目之间。这是为什么?我该如何摆脱它?

<ol data-bind='template: { name: "list_item_template", foreach: showable }'></ol>

<script type="text/html" id="list_item_template">
    <li class="listItem clearfix" id="list_item_${id}">
         ${title}
    </li>
</script>

结果是:

<ol data-bind='template: { name: "list_item_template", foreach: showable }'>
 <!---->
 <li class="listItem clearfix" id="list_item_301">Stuff</li> 
 <!---->
 <li class="listItem clearfix" id="list_item_302">Stuff</li> 
 <!---->
 <li class="listItem clearfix" id="list_item_303">Stuff</li> 
 <!---->
 <li class="listItem clearfix" id="list_item_304">Stuff</li>  
</ol>

谢谢

I'm using jQuery templates with Knockout, for some reason when using foreach, <!----> is magically inserted between every item. Why is that? And how do I get rid of it?

<ol data-bind='template: { name: "list_item_template", foreach: showable }'></ol>

<script type="text/html" id="list_item_template">
    <li class="listItem clearfix" id="list_item_${id}">
         ${title}
    </li>
</script>

This results in:

<ol data-bind='template: { name: "list_item_template", foreach: showable }'>
 <!---->
 <li class="listItem clearfix" id="list_item_301">Stuff</li> 
 <!---->
 <li class="listItem clearfix" id="list_item_302">Stuff</li> 
 <!---->
 <li class="listItem clearfix" id="list_item_303">Stuff</li> 
 <!---->
 <li class="listItem clearfix" id="list_item_304">Stuff</li>  
</ol>

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

暖风昔人 2024-12-19 05:17:44

一些想法:
(1) 您是否确认您使用的是最新版本的jquery.tmpl.js?

https://raw.github.com/jquery/jquery-tmpl /master/jquery.tmpl.js

我假设你的 viewModel 工作正常?这是我在本地测试中使用的视图模型。我猜你的看起来很相似?

var vm = {
    showable: ko.observableArray([
        { id: 0, title: 'foo' },
        { id: 1, title: 'bar' },
        { id: 2, title: 'baz' },
        { id: 3, title: 'bam' }
        ])
};

(2) 顺便说一句,我的理解是 Knockout 很快就会用 jsRender 取代 jquery.tmpl,所以希望你不要太执着于 .tmpl:

https://github.com/BorisMoore/jsrender

(3) 作为一种解决方法,您是否考虑过使用淘汰赛本身来代替模板的使用?这将消除您对 jquery.tmpl 的依赖。所以你的模板看起来像这样:

<script type="text/html" id="list_item_template">
    <li data-bind="text:title, attr:{id:'list_item_'+id}" class="listItem clearfix" ></li>
</script> 

A few thoughts:
(1) Have you confirmed that you are using the latest version of jquery.tmpl.js?

https://raw.github.com/jquery/jquery-tmpl/master/jquery.tmpl.js

And I assume your viewModel is working ok? Here's the viewmodel I used in my local test. I assume yours looks similar?

var vm = {
    showable: ko.observableArray([
        { id: 0, title: 'foo' },
        { id: 1, title: 'bar' },
        { id: 2, title: 'baz' },
        { id: 3, title: 'bam' }
        ])
};

(2) By the way, my understanding is that Knockout is soon going to replace jquery.tmpl with jsRender, so hopefully you're not too attached to .tmpl:

https://github.com/BorisMoore/jsrender

(3) As a workaround, have you considered replacing the use of templates with using knockout itself? This would remove your dependency on jquery.tmpl. So your template would look something like:

<script type="text/html" id="list_item_template">
    <li data-bind="text:title, attr:{id:'list_item_'+id}" class="listItem clearfix" ></li>
</script> 
滥情哥ㄟ 2024-12-19 05:17:44

我没有直接回答你的问题,但我有一个潜在的解决办法。

我不知道为什么 jQuery tmpl 在每个项目上注入注释。然而,我怀疑是否真的是 jQuery Tmpl 注入了注释,而不是 KnockoutJS。要进行验证,请尝试使用不带 KnockoutJS 的模板,看看 HTML 注释是否仍然出现。

最后,请注意最新版本的 Knockout (1.3 beta)有自己的原生模板引擎;您也许可以使用本机模板引擎来解决此问题。

I don't have a direct answer to your question, but I have a potential work around.

I don't know why jQuery tmpl injects a comment over each item. However, I'd question whether it really is jQuery Tmpl that's injecting the comment and not KnockoutJS. To verify, try using the template without KnockoutJS, see if the HTML comment still appears.

Finally, be aware that the latest version of Knockout (1.3 beta) has its own native template engine; you may be able to fix this by using the native template engine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文