在淘汰赛 1.3 中将选项传递给模板

发布于 2024-12-14 12:39:54 字数 577 浏览 3 评论 0 原文

在 knockoutjs 1.2.1 中我可以这样做:

<div data-bind="template: {name: 'Bar', foreach: persons, templateOptions:{fooMode: true} }"/>

<script id='Bar'>
    {{if $item.fooMode}} FOO! {{/if}}
</script>

我试图将其翻译为 knockout 1.3.0beta 但

<div data-bind="template: {name: 'Bar', foreach: persons, templateOptions:{fooMode: true} }"/>

<script id='Bar'>
    <span data-bind="if: $item.fooMode">FOO!</span>
</script>

新的本机模板引擎不尊重 templateOptions。

还有其他方法可以将任意数据传递到模板中吗?

In knockoutjs 1.2.1 I could do:

<div data-bind="template: {name: 'Bar', foreach: persons, templateOptions:{fooMode: true} }"/>

<script id='Bar'>
    {{if $item.fooMode}} FOO! {{/if}}
</script>

Which I have tried to translate to knockout 1.3.0beta as

<div data-bind="template: {name: 'Bar', foreach: persons, templateOptions:{fooMode: true} }"/>

<script id='Bar'>
    <span data-bind="if: $item.fooMode">FOO!</span>
</script>

But the new native template engine doesn't respect templateOptions.

Is there some other way I can pass arbitrary data into a template?

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

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

发布评论

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

评论(2

少女情怀诗 2024-12-21 12:39:54

正如您所发现的,本机模板引擎不支持 templateOptions,它是 jQuery 模板插件 options 功能的包装器。

您可以采取两种方式:
将数据放在视图模型上,并在模板内使用 $root.fooMode$parent.fooMode 。这将是最简单的选择。

否则,如果您不想在视图模型中使用该值,则可以使用自定义绑定来操作上下文,如下所示:

ko.bindingHandlers.templateWithOptions = {
    init: ko.bindingHandlers.template.init,
    update: function(element, valueAccessor, allBindingsAccessor, viewModel, context) {
        var options = ko.utils.unwrapObservable(valueAccessor());
        //if options were passed attach them to $data
        if (options.templateOptions) {
           context.$data.$item = ko.utils.unwrapObservable(options.templateOptions);
        } 
        //call actual template binding
        ko.bindingHandlers.template.update(element, valueAccessor, allBindingsAccessor, viewModel, context);
        //clean up
        delete context.$data.$item;
    } 
}

这是正在使用的示例: http://jsfiddle.net/rniemeyer/tFJuH/

请注意,在 foreach 场景中,您会发现您的选项$parent.$item 而不仅仅是 $item

As you discovered, the native template engine does not support templateOptions which was a wrapper to the jQuery Template plug-in's options functionality.

Two ways that you could go:
Place your data on your view model and use $root.fooMode or $parent.fooMode inside of your template. This would be the easiest option.

Otherwise, if you don't want the value in your view model, then you can use a custom binding to manipulate the context like:

ko.bindingHandlers.templateWithOptions = {
    init: ko.bindingHandlers.template.init,
    update: function(element, valueAccessor, allBindingsAccessor, viewModel, context) {
        var options = ko.utils.unwrapObservable(valueAccessor());
        //if options were passed attach them to $data
        if (options.templateOptions) {
           context.$data.$item = ko.utils.unwrapObservable(options.templateOptions);
        } 
        //call actual template binding
        ko.bindingHandlers.template.update(element, valueAccessor, allBindingsAccessor, viewModel, context);
        //clean up
        delete context.$data.$item;
    } 
}

Here is a sample in use: http://jsfiddle.net/rniemeyer/tFJuH/

Note that in a foreach scenario, you would find your options on $parent.$item rather than just $item.

分開簡單 2024-12-21 12:39:54

我建议桑德森的提案,您可以将新的文字传递给包含模型和额外数据(模板选项)的模板数据。

data-bind="template: { name: 'myTemplate', data: { model: $data, someOption: someValue } }"

工作演示 http://jsfiddle.net/b9WWF/

来源 https://github.com/knockout/knockout/issues/246#issuecomment-3775317

I would suggest Sanderson's proposal where you would pass new literal to template data that contains model and extra data (template options).

data-bind="template: { name: 'myTemplate', data: { model: $data, someOption: someValue } }"

Working Demo http://jsfiddle.net/b9WWF/

Source https://github.com/knockout/knockout/issues/246#issuecomment-3775317

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