knockoutjs - 渲染模板或 foreach 到自身

发布于 2024-12-14 13:57:57 字数 1354 浏览 0 评论 0原文

我最近一直在玩knockoutjs,看看它对我所做的事情是否有任何帮助。不过我有点坚持某件事。

假设我有一个 observableArray,并且我想使用模板绑定项目但没有容器,或者渲染自身。是否可以?

示例标记是:

<div class="header row">
    <div class="cell">Product Name</div>
    <div class="cell" data-bind="foreach: option_types">
        <div class="cell" data-bind="html: Name"></div>
    </div>
    <div class="cell">Level</div>
    <div class="cell">Infinite</div>
</div>

但是我真正想要的是这样的东西(注意没有子 .cell 元素和“renderSelf” - 组成的参数),

<div class="header row">
    <div class="cell">Product Name</div>
    <div class="cell" data-bind="foreach: option_types, renderSelf: true">
        ${Name}
    </div>
    <div class="cell">Level</div>
    <div class="cell">Infinite</div>
</div>

然后这会导致类似这样的结果

<div class="header row">
    <div class="cell">Product Name</div>
    <div class="cell">Name 1</div>
    <div class="cell">Name 2</div>
    <div class="cell">Name 3</div>
    <div class="cell">Name 4</div>
    <div class="cell">Level</div>
    <div class="cell">Infinite</div>
</div>

吗?或者我以错误的方式思考它?

谢谢。

I've been playing around with knockoutjs recently to see if it's going to be of any help to the things I do. I'm a bit stuck on something though.

Say I have an observableArray and and I want to bind the items using a template but without a container, or render itself. Is it possible?

The sample markup is:

<div class="header row">
    <div class="cell">Product Name</div>
    <div class="cell" data-bind="foreach: option_types">
        <div class="cell" data-bind="html: Name"></div>
    </div>
    <div class="cell">Level</div>
    <div class="cell">Infinite</div>
</div>

However what I would really want is something like this (note no child .cell elements, and "renderSelf" - made up parameter)

<div class="header row">
    <div class="cell">Product Name</div>
    <div class="cell" data-bind="foreach: option_types, renderSelf: true">
        ${Name}
    </div>
    <div class="cell">Level</div>
    <div class="cell">Infinite</div>
</div>

Which would then result in something like

<div class="header row">
    <div class="cell">Product Name</div>
    <div class="cell">Name 1</div>
    <div class="cell">Name 2</div>
    <div class="cell">Name 3</div>
    <div class="cell">Name 4</div>
    <div class="cell">Level</div>
    <div class="cell">Infinite</div>
</div>

Is that going to be possible? Or am I thinking of it in the wrong way?

Thanks.

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

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

发布评论

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

评论(1

美人迟暮 2024-12-21 13:57:57

版本 1.3 是“稳定测试版”,允许您声明所谓的无容器控制流。这是一种奇特的说法,您可以在 HMTL 注释中声明 foreach,这将允许您实现所需的输出。对于您的示例,它看起来像这样:

<div class="header row">
    <div class="cell">Product Name</div>
    <!-- ko foreach: option-types -->
    <div class="cell" data-bind="html: Name"></div>
    <!-- /ko -->
    <div class="cell">Level</div>
    <div class="cell">Infinite</div>
</div>

请参阅 请参阅 Steve Sanderson 博客上的这篇文章了解更多信息。

我最近在一个比较大的应用程序上使用了1.3.0,发现它非常稳定。

顺便说一句,1.3.0 中还可以在 foreach 模板中访问父视图模型和根视图模型,这一功能非常有用,有助于保持视图模型设计更加简洁。

Version 1.3 which is "stable beta" allows you to declare what's called a Containerless Control Flow. This is a fancy way of saying you can declare the foreach within an HMTL comment which will allow you to achieve the desired output. For your example it would look like this:

<div class="header row">
    <div class="cell">Product Name</div>
    <!-- ko foreach: option-types -->
    <div class="cell" data-bind="html: Name"></div>
    <!-- /ko -->
    <div class="cell">Level</div>
    <div class="cell">Infinite</div>
</div>

See this article on Steve Sanderson's blog for more info.

I've been using 1.3.0 on a relatively large application recently and found it to be very stable.

As an aside the ability in 1.3.0 to also access the parent and root viewModel within a foreach template is incredibly useful and helps keep your viewModel design much cleaner.

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